This time, we mainly explain the pages of the obtained data after logging in. First, we create a new login page login.jsp. Because we mainly learn paging, the login verification part will not be explained. The main code is as follows:
<form action="pageServlet">Username:<input type="text" name="username"><br>Password:<input type="text" name="password"><br><input type="submit" value="submit"></form>
First, create the entity class User.java and add get and set methods:
public class User {private String username;private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}We can see that the form form is submitted to the pageServlet, so we create a new PageServlet and get the data in the Servlet, and make some pagination preparations. The specific meaning can be understood by referring to the comments. PageServlet code:
public class PageServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {List<User> list = new ArrayList<User>();// Here I no longer connect to the database but use virtual data to test the effect. Friends can connect to the database and query it and return a listfor (int i = 1; i < 7; i++) {User user1 = new User();user1.setUsername("Th" + i + "Username");user1.setPassword("Th" + i + "Password");list.add(user1);}HttpSession session = request.getSession();//Save data in session to facilitate the acquisition of session.setAttribute("userList", list);//Get the number of pages of the current page and convert it to int type, and finally store the data in session int pageNos; if (request.getParameter("pageNos") == null|| Integer.parseInt(request.getParameter("pageNos")) < 1) {pageNos = 1;} else {pageNos = Integer.parseInt(request.getParameter("pageNos"));}session.setAttribute("pageNos", pageNos);// Define the total number of pages and store it in session int countPage = 3;// In actual development, our total number of pages can be obtained from the total number of pages based on the SQL statement, and then divide the number of pieces per page to get the total number of pages session.setAttribute("countPage", countPage);request.getRequestDispatcher("index.jsp").forward(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}}In the above code, we will eventually forward to the index.jsp page. At this time, all our data will be displayed in index.jsp and obtained using JSTL and EL expressions. The main code of index.jsp is as follows:
<body><c:forEach items="${userList}" var="user" begin="${(pageNos-1)*2 }"end="${pageNos*2-1}"><center><div>${user.username}</div></center><center><div>${user.password}</div></center></c:forEach><center><c:if test="${pageNos>1 }"><a href="pageServlet?pageNos=1" >Home</a><a href="pageServlet?pageNos=${pageNos-1 }">Previous Page</a></c:if><c:if test="${pageNos <countPage }"><a href="pageServlet?pageNos=${pageNos+1 }">Next page</a><a href="pageServlet?pageNos=${countPage }">Last page</a></c:if></center><form action="pageServlet"><h4 align="center">Total ${countPage} page<input type="text" value="${pageNos}" name="pageNos" size="1">Page<input type="submit" value="go"></h4></form></body>In the second line, we use <c:forEach> to get the content in session.setAttribute();. Note that here, I default to two pieces of data per page, so it is (pageNos-1)*2. If N pieces of data per page, you need to change 2 to N. Of course, N can also be obtained from the background servlet.
At the same time, because we use JSTL expressions in index.jsp, remember to import the reference:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
At this point, we have completed a simple pagination, go and try it.
The above is the complete code of simple pagination in JavaWeb introduced to you (recommended). I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!