Implementation code example of Java Web paging

Time:2021-4-7

This article mainly introduces the implementation of Java Web paging code examples, this article through the example code is very detailed, for everyone’s study or work has a certain reference learning value, need friends can refer to

Classification of Pagination

Pagination is divided into true pagination and false pagination.

1. True paging (physical paging)

Implementation principle: select * from XXX [where…] limit?, 10;

The first parameter is the index position of the starting data

10 is how many pieces of data to query, that is, the number of pieces displayed on each page

Advantage: no memory overflow

Disadvantages: page turning speed is relatively slow

2. False pagination (logical pagination)

Implementation principle: query all the data in the memory at one time, and take out the data of the corresponding index interval directly from the memory every time you need to query

Advantages: paging speed is relatively fast

Disadvantages: may cause memory overflow

Some terms of pagination:

  • –Total number of data: totalcount: select count (1) from t_ user;
  • –Number of display per page: PageSize
  • –Page total
  • –Current page: currpage
  • –Start index: StartIndex

–Through the current page number query page data

select * from t_ User limit 0, 5; — page 1
select * from t_ User limit 5, 5; — page 2
select * from t_ User limit 10, 5; — page 3

–Formula: StartIndex = (currpage – 1) * PageSize

–How many pages are there

–Method 1: result = totalcount% PageSize, if the remainder result is 0,

— totalPage = totalCount / pageSize

–If the remainder result is not 0,

— totalPage = totalCount / pageSize + 1;

–Method 2: totalpage = (totalcount + PageSize – 1) / PageSize

Paging tool class


public class PaginationBean<T> {
  
  private List<T> dataList;
  
  private int currPage;
  
  private int totalPage;

  public List<T> getDataList() {
    return dataList;
  }

  public void setDataList(List<T> dataList) {
    this.dataList = dataList;
  }

  public int getCurrPage() {
    return currPage;
  }

  public void setCurrPage(int currPage) {
    this.currPage = currPage;
  }

  public int getTotalPage() {
    return totalPage;
  }

  public void setTotalPage(int totalPage) {
    this.totalPage = totalPage;
  }
}

Servlet

@WebServlet("/showUserList")
public class ShowUserListServlet extends HttpServlet implements Servlet {
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    String operation = request.getParameter("operation");
    String currPageStr = request.getParameter("currPage");
    int currPage = 0;
    
    IUserService userSevice = new UserServiceImpl();
    int totalPage = userSevice.getTotalPage();
    
    If ("home". Equals (operation) | operation = = null | currpagestr = = null|| currPageStr.length () == 0) {
      
      currPage = 1;
    }Else if ("previous page". Equals (operation)){
      
      currPage = Integer.parseInt(currPageStr) - 1;
      if (currPage <= 0) {
        currPage = 1;
      }
    }Else if ("next page". Equals (operation)){
      
      currPage = Integer.parseInt(currPageStr) + 1;
      if (currPage >= totalPage) {
        currPage = totalPage;
      }
    } else {
      
      currPage = totalPage;
    }
    
    List<TestUserBean> userList = userSevice.getUserListByCurrPage(currPage);
    
    PaginationBean<TestUserBean> pageBean = new PaginationBean<TestUserBean>();
    pageBean.setDataList(userList);
    pageBean.setCurrPage(currPage);
    pageBean.setTotalPage(totalPage);
    
    request.setAttribute("page", pageBean);
    
    request.getRequestDispatcher("/userList.jsp").forward(request, response);
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
  }

}

jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<% -- introduce JSTL --% >  
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
  table {
    border-collapse: collapse;
  }
</style>
</head>
<body>
  <table border="1">
    <tr>
      <th>ID</th>
      < th > name < / th >
      < th > password < / th >
      <th> ID number </th>
    </tr>
    <c:forEach items="${page.dataList }" var="user">
      <tr>
        <td>${user.id }</td>
        <td>${user.name }</td>
        <td>${user.pwd }</td>
        <td>${user.idCard }</td>
      </tr>
    </c:forEach>
  </table>
  <span>The third${ page.currPage  }Page of${ page.totalPage  }Page</span>
  <br>
  <br>
  <form action="showUserList" method="get">
    < input type = submit "name = operation" value = home page ">
    < input type = submit "name = operation" value = previous ">
    < input type = submit "name = operation" value = next ">
    < input type = submit "name = operation" value = last page ">
    
    <input type="hidden" name="currPage" value="${page.currPage }">
  </form>
</body>
</html>

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.