黑马程序员技术交流社区
标题:
【成都校区】根据页码查询数据库servlet
[打印本页]
作者:
幻觉
时间:
2019-1-3 12:43
标题:
【成都校区】根据页码查询数据库servlet
package cn.itcast.web;
import cn.itcast.domain.PageBean;
import cn.itcast.domain.User;
import cn.itcast.service.Impl.ServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/*总页数:pageTotal userTotal%row==0? userTotal/row : userTotal/row +1
* 每页查询用户数:row 5
* 每页开始的索引 indexStart = (当前页数-1)*每页查询数
* */
@WebServlet("/findByPageServlet")
public class FindByPageServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置下编码
request.setCharacterEncoding("utf-8");
//接收当前页数thisPage及每页条数row 以及一个参数的集合
Map<String, String[]> map = request.getParameterMap();
String thisPage = request.getParameter("thisPage");
String row = request.getParameter("row");
//数据库中查询用户的总数:totalCount 33
ServiceImpl service = new ServiceImpl();
if ( thisPage==null || "".equals(thisPage)){
thisPage="1";
}
if (row!="8"||"".equals(row)){
row="8";
}
//数据库中查询当前页用户的信息
PageBean<User> findPage= service.findPageUsers(thisPage,row,map);
//将数据转发
request.setAttribute("map",map);
request.setAttribute("findPage",findPage);
request.getRequestDispatcher("/list.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2