Simple paging in Java Web, simple paging in Java Web

Source: Internet
Author: User

Simple paging in Java Web, simple paging in Java Web

This article mainly explains how to paging the data obtained after logon. First, we create a login page login. jsp, because we mainly learn about paging, the login verification section is not described. The main code is as follows:

1 <form action = "pageServlet"> 2. username: <input type = "text" name = "username"> <br> 3. Password: <input type = "text" name = "password"> <br> 4 <input type = "submit" value = "submit"> 5 </form>

First, create the object class User. java and add the get and set methods:

 1 public class User { 2     private String username; 3     private String password; 4     public String getUsername() { 5         return username; 6     } 7     public void setUsername(String username) { 8         this.username = username; 9     }10     public String getPassword() {11         return password;12     }13     public void setPassword(String password) {14         this.password = password;15     }16     17 }

We can see that the form is submitted to pageServlet, So we create a new PageServlet, get the data in the Servlet, and make some paging preparation. For details, refer to the annotations, pageServlet code:

1 public class PageServlet extends HttpServlet {2 public void doGet (HttpServletRequest request, HttpServletResponse response) 3 throws ServletException, IOException {4 List <User> list = new ArrayList <User> (); 5 // here I will not connect to the database but use virtual data to test the effect. My friends can connect to the database and query it, and then return a list 6 for (int I = 1; I <7; I ++) {7 User user1 = new User (); 8 user1.setUsername ("no." + I + "username "); 9 user1.setPassword ("password"); 10 list. add (user1); 11} 12 HttpSession session = request. getSession (); 13 // Save the data to the session to facilitate obtaining 14 sessions at the front end. setAttribute ("userList", list); 15 // get the number of pages on the current page and convert it to the int type. Finally, the data is saved to the session 16 int pagnos; 17 if (request. getParameter ("pagnos") = null18 | Integer. parseInt (request. getParameter ("pagnos") <1) {19 pagnos = 1; 20} else {21 pagnos = Integer. parseInt (request. getParameter ("pagnos"); 22} 23 session. setAttribute ("pagnos", pagnos); 24 // defines the total number of pages to coexist in the session 25 int countPage = 3; 26 // in actual development, the total number of pages can be queried Based on the SQL statement. Then, the total number of pages can be obtained by dividing the number of pages by the total number of sessions. setAttribute ("countPage", countPage); 28 request. getRequestDispatcher ("index. jsp "). forward (request, response); 29} 30 31 public void doPost (HttpServletRequest request, HttpServletResponse response) 32 throws ServletException, IOException {33 34} 35 36}

In the above Code, we will finally forward it to index. jsp page, all our data will be displayed in index. in jsp, obtained by using the JSTL and EL expressions, index. the main code of jsp is as follows:

1 <body> 2 <c: forEach items = "$ {userList}" var = "user" begin = "$ {(pageNos-1) * 2} "3 end =" $ {pagnos * 2-1} "> 4 <center> 5 <div >$ {user. username} </div> 6 </center> 7 <center> 8 <div >$ {user. password} </div> 9 </center> 10 </c: forEach> 11 <center> 12 <c: if test = "$ {pagnos> 1}"> 13 <a href = "pageServlet? Pagnos = 1 "> homepage </a> 14 <a href =" pageServlet? Pagnos =$ {pageNos-1} "> previous page </a> 15 </c: if> 16 <c: if test = "$ {pagnos <countPage}"> 17 <a href = "pageServlet? Pagnos =$ {pagnos + 1} "> next page </a> 18 <a href =" pageServlet? Pagnos =$ {countPage} "> last page </a> 19 </c: if> 20 </center> 21 <form action = "pageServlet"> 22 

In the second row, we use <c: forEach> to obtain 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 needs to change 2 to N, of course, N can also be obtained from the backend Servlet.

At the same time, because we use a 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. Please try it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.