Handwritten paging summary
-
-
-
- Paging technology
-
-
-
-
- Front-end paging
-
-
- Backend paging
-
- Implement paging
-
-
- Preparation work
-
-
- Effect drawing
-
- Core code
Running environment: tomcat8.0+idea
Technology: servlet+jsp+javabean+mysql
1. Paging technology
1. Front-end paging
Front-end paging: Read all the data in the table from the database at one time and return it to the client at one time. The front-end js controls the display of each page. Since all the data is read out at one time and returned to the client, if the amount of data is huge, This action may consume a lot of server resources and bandwidth, but it will be very easy after returning to the client. The client will no longer request resources from the server for a period of time.
2. Backend paging
This article paging is a type of back-end paging implementation.
Back-end paging: controlled by the back-end program, only one page of data is returned each time, and is returned and displayed to the client. The advantage is that it reduces the time of a single query of the database. The back-end paging method requires frequent interaction with the server. , because of frequent interactions, it will put a burden on the server.
2. Implement paging
1. Preparation work
Before making paging, you first need to understand what are the properties of paging?
- Current page number
currentPage
- Total number of pages
totalPage
- Total number of records
totalSize
- Number of records per page
pageSize
- Current page data
pageList
- etc…
We can turn these properties into properties in a JavaBean to call, and implement some paging-related business logic.
Encapsulate data about the number of pages:
Request data: Current page number currentPage
Response data: PageBean class encapsulation (including the properties we mentioned above)
Total number of pages = (Total number of records % Number of records per page == 0) ? (Total number of records / Number of records per page) : (Total number of records / Number of records per page + 1)
;
Current page data list = query(select * from limit number of records per page*(number of current pages-1), number of records per page
);
2. Core code
bean PageBean.java
package com.sun.bean;
import com.sun.util.DBConnection;
import com.sun.util.Date_String;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author JumperSun
* @date 2022-11-15-8:31
*/
public class PageBean<T> {
private Integer currentPage; // current page number
private Integer totalPage; // total pages
private Integer totalSize; // total
private Integer pageSize; // Number of records per page
private List<T> list; // Current page data <T>
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Integer getTotalSize() {
return totalSize;
}
public void setTotalSize(Integer totalSize) {
this.totalSize = totalSize;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
// Total number of pages
public int size()throws SQLException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = DBConnection.getConnection();
String sql = "select count(*) from user";
st = conn.createStatement();
rs = st.executeQuery(sql);
if(rs.next()) {
return rs.getInt(1);
}
} finally {
DBConnection.close(rs, st, conn);
}
return 0;
}
// Query data by page
public List<UserBean> queryAllByLimit(int offset, int limit) throws SQLException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<UserBean> users=new ArrayList<UserBean>();
try {
conn = DBConnection.getConnection();
String sql = "select * from user limit " + offset + "," + limit;
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()==true){
UserBean tmp=new UserBean();
tmp.setUserid(rs.getInt("id"));
tmp.setUsername(rs.getString("name"));
tmp.setPassword(rs.getString("password"));
tmp.setSex(rs.getString("sex"));
tmp.setAge(rs.getInt("age"));
String birthday= Date_String.getStringDate1(rs.getDate("birthday"));
tmp.setBirthday(birthday);
users.add(tmp);
}
} finally {
DBConnection.close(rs, ps, conn);
}
return users;
}
// Pagination query processing
public void selectUserListByPage(Integer currentPage, PageBean<UserBean> pageBean) throws SQLException {
// current page number
pageBean.setCurrentPage(currentPage);
// total
Integer totalSize = pageBean.size();
pageBean.setTotalSize(totalSize);
// Number of records per page
Integer pageSize = 3;
pageBean.setPageSize(pageSize);
// total pages(No remainder is a full page,Number of pages remaining+1)
Integer totalPages = null;
if (totalSize != null) {
totalPages = (totalSize%pageSize==0) ? (totalSize/pageSize) : (totalSize/pageSize+1);
}
if(totalPages != null) {
pageBean.setTotalPage(totalPages);
}
// Current page data
List<UserBean> list = queryAllByLimit(pageSize*(currentPage-1), pageSize);
pageBean.setList(list);
}
}
Servlet GetUserListByPage.java
package com.sun.servlet;
import java.io.IOException;
import java.sql.SQLException;
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 com.sun.bean.PageBean;
import com.sun.bean.UserBean;
@WebServlet("/getUserListByPage")
public class GetUserListByPage extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String currentPageStr = request.getParameter("currentPage");
Integer currentPageNum = getCurrentPage(currentPageStr);
PageBean<UserBean> pageBean = new PageBean<>();
try {
pageBean.selectUserListByPage(currentPageNum,pageBean);
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("/Main.jsp").forward(request,response);
}
private Integer getCurrentPage(String currentPagestr) {
if (null == currentPagestr) {
currentPagestr = "1";
}
return Integer.valueOf(currentPagestr);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
jsp Main.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" errorPage="../error.jsp" %>
<%@ page import="java.util.List" %>
<%@ page import="com.sun.bean.UserBean" %>
<%@ page import="com.sun.bean.PageBean" %>
<!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">
<link rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" />
</head>
<body>
<div class="content">
<jsp:useBean id="pageBean" class="com.sun.bean.PageBean" scope="request"/>
<table class="mtable2">
<h3 style="margin-bottom: 0">student list</h3>
<form action="{pageContext.request.contextPath}/getUserListByPageLike" method="post">
student name:<input type="text" name="username" value="{param.username}"/>
gender:<input type="text" name="sex" value="{param.sex}"/>
<input type="submit" value="Inquire"/>
<input type="reset" value="reset">
</form>
<tr>
<th>studentid</th>
<th>student name</th>
<th>password</th>
<th>gender</th>
<th>age</th>
<th>date of birth</th>
<th>operate</th>
</tr>
<c:forEach items="{pageBean.list}" var="user">
<tr>
<td>{user.getUserid()}</td>
<td>{user.getUsername()}</td>
<td>{user.getPassword()}
</td>
<td>{user.getSex()}
</td>
<td>{user.getAge()}
</td>
<td>{user.getBirthday()}
</td>
<td>
<a >Revise</a>
<a >delete</a>
</td>
</tr>
</c:forEach>
</table>
<div class="fenye">
No.{pageBean.currentPage}/{pageBean.totalPage}Page
total:{pageBean.totalSize}strip
per page{pageBean.pageSize}strip
<c:if test="{pageBean.currentPage != 1}">
<a >
[front page]
</a>
<a >
[Previous page]
</a>
</c:if>
<c:if test="{pageBean.currentPage != pageBean.totalPage}">
<a >
[Next page]
</a>
<a >
[last page]
</a>
</c:if>
</div>
</div>
</body>
</html>
**Yours sincerely, I finished it in one page. **