1,创建分页的实体类: 然后获得其属性的get和set方法   需要注意的是getTotalPage()方法需要修改下,计算出总页数


public  class UtilPage {
 
   public int  totalPage;// 总页数
 
   public int  rowsCount;// 总条数
 
   public int  currentPage;// 当前页数
 
   public  final  int pageSize = 3;// 页面显示条数
 
   public int  getTotalPage() {
 
      if (rowsCount %  pageSize == 0) {
 
         totalPage = (rowsCount /  pageSize);
 
      } else  {
 
         totalPage = (rowsCount /  pageSize + 1);
 
      }
 
      return totalPage;
 
   }
 
  2,在Dao层中创建两个接口,然后再实现:  获取需要分页的数据(这个没啥难的)
 
        public List<Customer> getCustomer(int currentPage, int  pageSize) {
 
      List<Customer> list  = new ArrayList<>(0);
 
      ct = JDUtil.getConnection();
 
      String sql  = "select * from t_customer c INNER JOIN t_dict d on c.custLevel=d.did limit ?,?";
 
      try {
 
         ps =  ct.prepareStatement(sql);
 
         ps.setInt(1, (currentPage-1)*pageSize);
 
         ps.setInt(2, pageSize);
 
         rs =  ps.executeQuery();
 
         while (rs.next()) {
 
        代码
 
         }
 
      } catch  (Exception e) {
 
         e.printStackTrace();
 
      } finally {
 
         JDUtil.closeAll(ct, ps, rs);
 
      }
 
      return list;
 
   }
 
  //获取查询数据的总条数
 
   public int  selectRowsCount() {
 
      int rowsCount=0;
 
      ct = JDUtil.getConnection();
 
      String sql  = "select count(*) from t_customer";
 
      try {
 
         ps=ct.prepareStatement(sql);
 
         rs=ps.executeQuery();
 
         while(rs.next()){
 
            rowsCount=rs.getInt(1); 
 
         }
 
      } catch  (Exception e) {
 
         e.printStackTrace();
 
      }finally {
 
         JDUtil.closeAll(ct, ps, rs);
 
      }
 
      return rowsCount;
 
   }
 
}


3,在service层中建立接口并实现:(我这是建立一个Map集合,一起存储需要分页的数据和数据总条数,也可以单独实现)


public Map<String, Object> getLimitCustomer(int currentPage, int  pageSize) {
 
      Map<String,Object> map=new HashMap<>();
 
      List<Customer> list  = cd.getCustomer(currentPage, pageSize);
 
      int rowsCount  = cd.selectRowsCount();
 
      map.put("list", list);
 
      map.put("rowsCount", rowsCount);
 
      return map;
 
   }
 
4,在control层中进行具体的Servlet处理
 
      UtilPage utilPage  = new UtilPage();(这个是分页实体类对象)
 
      //获取当前页面
 
      int currentPage  = Integer.parseInt(request.getParameter("currentPage"));
 
      Map<String, Object> limitCustomer  = cs.getLimitCustomer(currentPage, utilPage.getPageSize());(利用对象调用service层的方法)
 
      //获取客户集合
 
      List<Customer> list  =(List<Customer>)limitCustomer.get("list");
 
      int rowsCount  =(int)limitCustomer.get("rowsCount");
 
      //将当前页面和总数据条数赋给对象
 
      utilPage.setRowsCount(rowsCount);
 
      utilPage.setCurrentPage(currentPage);
 

 
      request.setAttribute("utilPage", utilPage);
 
      request.setAttribute("list", list);
 
        //请求转发
 
        try {
 
         request.getRequestDispatcher("WEB-INF/customer/limitPage.jsp").forward(request, response);
 
      } catch  (Exception e) {
 
         // TODO Auto-generated catch block
 
         e.printStackTrace();
 
      }


    5, 下边就是请求转发到所需要的jsp页面中的分页栏


共[<font color="red"><B>${utilPage.rowsCount}</B></font>]条记录,共[<font color="red"><B>${utilPage.totalPage}</B></font>]页,当前第[<font color="red">              <B>${utilPage.currentPage}</B></font>]页
 
               <c:if test="${utilPage.currentPage!=1}">
 
               [<B><a href="Customer?C=getLimitCustomer¤tPage=1">首页</a></B>]
 
               [<B><a href="Customer?C=getLimitCustomer¤tPage=${utilPage.currentPage-1}">前一页</a></B>]
 
               </c:if>
 
               <c:if test="${utilPage.currentPage!=utilPage.totalPage}">
 
               [<B><a href="Customer?C=getLimitCustomer¤tPage=${utilPage.currentPage+1}">后一页</a></B>]
 
               [<B><a href="Customer?C=getLimitCustomer¤tPage=${utilPage.currentPage=utilPage.totalPage}">尾页</a></B>]
 
               </c:if>


这是主要的实现思想和代码,只要有web基础的都能看明白啥意思,不是完整的程序!!!