English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This time, we will mainly explain how to paginate the data obtained after logging in. First, we create a login page called login.jsp. Since we mainly want to learn about pagination, the part of login verification will not be elaborated on, and 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>
Firstly, establish 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 is submitted to the PageServlet, so we create a new PageServlet, obtain the data in the Servlet, and prepare some pagination. The specific meaning can be understood by referring to the comments, the code of PageServlet is as follows:
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 for testing the effect, friends can connect to the database, query the data, and return a list for (int i = 1; i < 7; i++) { User user1 = new User(); user1.setUsername("the" + i + "user name"); user1.setPassword("the" + i + "password"); list.add(user1); } HttpSession session = request.getSession(); // Store the data in the session for easy access on the front end session.setAttribute("userList", list); //Obtain the current page number and convert it to an int type, and finally store the data in the session int pageNos; if (request.getParameter("pageNos") == null || Integer.parseInt(request.getParameter("pageNos")) < 1) { pageNos = 1; } pageNos = Integer.parseInt(request.getParameter("pageNos")); } session.setAttribute("pageNos", pageNos); // Define the total number of pages and store it in the session int countPage = 3; // In actual development, the total number of pages can be obtained by querying the total number of rows with SQL statements, and then the total number of pages is obtained by dividing the total number of rows by the number of rows per page. 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 finally redirect to the index.jsp page, at this time all our data will be displayed in the index.jsp page, obtained by 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="pageServlet63;pageNos=1" >Home</a>/a> <a href="pageServlet63;pageNos=${pageNos}-1 ">Previous Page</a>/a> </c:if> <c:if test="${pageNos < countPage }"> <a href="pageServlet63;pageNos=${pageNos}+1 ">Next Page</a>/a> <a href="pageServlet63;pageNos=${countPage }">Last Page</a> </c:if> </center> <form action="pageServlet"> h4 align="center">Total ${countPage} pages <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 obtain the content of session.setAttribute();. Note that here I assume it is two items per page, so it is (pageNos-1)*2, if there are N items per page, then2Change to N, of course, N can also be obtained from the background Servlet.
At the same time, because we used JSTL expressions in index.jsp, remember to import the reference:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
That's it, we have completed a simple pagination. Go and try it out quickly.
The above is the complete code for simple pagination in JavaWeb introduced by the editor to everyone. I hope it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time. I am also very grateful for everyone's support for the Yelling tutorial website!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)