Simple paging in JavaWeb is recommended by the full code of

  • 2020-05-12 02:45:57
  • OfStack

First, we will create a new login page, login.jsp. Since we mainly learn about pagination under 1, we will not elaborate on the part of login verification. The main code is as follows:


<form action="pageServlet">
 The user name :<input type="text" name="username"><br>
 The secret   code :<input type="text" name="password"><br>
<input type="submit" value=" submit ">
</form>

First, create the entity class User.java and add the 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 pageServlet, so we will create a new PageServlet and get the data in Servlet. At the same time, we will make some preparation for pagination. The specific meaning can be understood by referring to the comments.


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 the virtual data to test the effect, the partner can connect to the database query to return 1 a list
for (int i = 1; i < 7; i++) {
User user1 = new User();
user1.setUsername(" The first " + i + " A user name ");
user1.setPassword(" The first " + i + " password ");
list.add(user1);
}
HttpSession session = request.getSession();
//  Save the data to session In order to get at the front desk 
session.setAttribute("userList", list);
// Gets the number of pages of the current page and converts to int type , Finally, I'm going to save the data to session In the 
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 to coexist session In the 
int countPage = 3;
//  In actual development our total number of pages can be based on sql Statement gets the total number of bars queried, and then divides the total number of bars by the number of bars 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, which can be obtained with 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 page </a>
<a href="pageServlet?pageNos=${pageNos-1 }"> on 1 page </a>
</c:if>
<c:if test="${pageNos <countPage }">
<a href="pageServlet?pageNos=${pageNos+1 }"> Under the 1 page </a>
<a href="pageServlet?pageNos=${countPage }"> At the end of the page </a>
</c:if>
</center>
<form action="pageServlet">
<h4 align="center"> A total of ${countPage} page  
<input type="text" value="${pageNos}" name="pageNos" size="1"> page 
<input type="submit" value="go">
</h4>
</form>
</body>

We're going to use it in line 2 < c:forEach > The session. setAttribute (); The content is retrieved. Note that here I default to two data per page, so it is (pageNos-1)*2. If there are N data per page, then I need to change 2 to N. Of course, N can also be obtained from background Servlet.

Also, since we used the JSTL expression in index.jsp, remember to import the reference:


<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

Here we have completed a simple page, go to try it.


Related articles: