Implementation code of JSP pagination display

Time:2020-11-19

In recent days in the process of JSP message board design, encountered a problem. Let’s look at a screenshot:
Implementation code of JSP pagination display

This is a random message in a news page cut map, if the number of messages is too much, the entire page must be arranged long, which directly caused trouble to users, uncomfortable feelings, so, to solve this problem, usually use the method of pagination display.
To design the page display mode in this way, we usually need to use these basic variables: PageSize (number of records displayed on each page), pagecount (total number of pages), showpage (number of pages displayed at present), and RecordCount (total number of records). In order to facilitate understanding, a figure is drawn

Implementation code of JSP pagination display

If you want to display the page in this way, PageSize = 4, pagecount = 3, showpage = 1, RecordCount = 12. The idea is that if you want to display that page, you need to calculate the first record of each page is the number of records in all records. If the first record of each page is the position record of the total record, then position = (showpage – 1) × PageSize + 1. For example, in the example above, if you want to display the first page, you need to calculate that the first record in the first page is the first record in the total record; if you want to display the second page, you need to calculate that the first record in the second page is the fourth record in the total record; if you want to display the third page, you need to calculate that the first record on the first page is the ninth record in the total record Records.
The core code in JSP is as follows (the database used is MySQL)

Copy codeThe code is as follows:
<%!  int pageSize=4;
 int pageCount;
 int showPage;
 %>

<! — connect to the database and retrieve records from the database > >
<%
 Connection con;
 Statement sql;
 ResultSet rs;

 try{Class.forName(“com.mysql.jdbc.Driver”);
 }catch(ClassNotFoundException e){
 }

 try{con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/message board”,”root”,”123456″);
  sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
/ / returns a scrollable result set
  rs=sql.executeQuery(“select * from messageinfo”);
/ / move the cursor to the last line
  rs.last();
/ / get the line number of the last line
  int recordCount=rs.getRow();
/ / calculate the total number after Pagination
  pageCount=(recordCount%pageSize==0)?(lastRow/pageSize):(lastRow/pageSize+1);

/ / get the number of pages the user wants to display:
  String integer=request.getParameter(“showPage”);
  if(integer==null){
   integer=”1″;
  }
  try{showPage=Integer.parseInt(integer);
  }catch(NumberFormatException e){
   showPage=1;
  }
  if(showPage<=1){
   showPage=1;
  }
  if(showPage>=pageCount){
   showPage=pageCount;
  }

/ / if you want to display the showpage page page, the value of position the cursor should move to is:
  int position=(showPage-1)*pageSize+1;
/ / set the cursor position
  rs.absolute(position);
/ / use the for loop to display the records that should be displayed on this page
  for(int i=1;i<=pageSize;i++){  
 %>
   <table>
    <tr>
     <th><%=rs.getString(“UserName”) %></th>
< td > published in: <%= rs.getString (“datetime”) %></td>
    </tr>
    <tr >
     <th colspan=”3″><textarea><%=rs.getString(“content”) %></textarea></th>
    </tr>
   </table>

 <%  
   rs.next();
  } 
  rs.close();
  con.close();
  }
  catch(Exception e){
  e.printStackTrace();}
 %>
 <br>
Page <% = showpage% > of <% = pagecount% >
 <br>
 <a href=” ShowMessages.jsp?showPage=1 “> Home Page</a>
 <a href=” ShowMessages.jsp?showPage= <% = showpage-1% > “> previous page</a>
<% / / display the number of each page according to the value of pagecount and attach the corresponding hyperlink
  for(int i=1;i<=pageCount;i++){
 %>
   <a href=”ShowMessages.jsp?showPage=<%=i%>”><%=i%></a>
<% }
 %> 
 <a href=” ShowMessages.jsp?showPage= <% = showpage + 1% > “> next page</a>
 <a href=” ShowMessages.jsp?showPage= <% = pagecount% > “> last page</a>
<! — submit the number of pages the user wants to display through the form — >
 <form action=”” method=”get”>
Go to < input type = “text” name = “showpage” size = “4” >
< input type = submit “name =” submit “value = jump” >
 </form> 

The running results are as follows (in order to simplify the code, the code related to web page layout has been removed, here only refers to the function)

Implementation code of JSP pagination display

You can jump to the first page, the previous page, the next page and the last page. You can manually specify the number of pages on the number, or you can input the number of pages to be displayed in the input box.

Recommended Today

What are the good recommendations of project management software?

With the continuous expansion of the enterprise scale, project management has become the biggest challenge and problem, there will be a series of problems, such as: resource consumption out of control, and the communication between project departments and relevant departments is becoming more and more difficult, team cohesion decline, project progress is difficult to control, […]