Implementation of JSP paging display code _JSP programming

Source: Internet
Author: User
Tags prev

In the past few days in doing JSP message board design process, encountered a problem. First look at a screenshot:

This is casually in a News page section of the picture, if the number of message bar too much, that the whole page has to line up long, this directly to the user caused trouble, uncomfortable feelings, so, to solve this problem, usually using the method of pagination display.
To design the page display in such a way, you usually need to use these basic variables: pageSize (number of records per page), PageCount (how many pages are in total), ShowPage (currently showing page), RecordCount (total number of records), To facilitate understanding, draw a picture:

If you want to display the page as such an effect, pagesize=4,pagecount=3,showpage=1,recordcount=12 in this picture. The idea is that if you want to show that page, you have to figure out the first record of each page is a few records in all records, assuming that the first record of each page is the position record in the total record, then position= (ShowPage-1) xpagesize+1. Like the example above, to display the first page, 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, 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, The first record in the first page is calculated as the Nineth record in the total record.
The core code in the JSP is as follows (the database used is MySQL):

Copy Code code as follows:

<%! int pagesize=4;
int PageCount;
int showpage;
%>

<!--connect the database and fetch 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 row
Rs.last ();
Gets the line number of the last line
int Recordcount=rs.getrow ();
Calculate the total number of pages after paging
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 page showpage, the position value that the cursor should move to is:
int position= (showPage-1) *pagesize+1;
To set the position of a cursor
Rs.absolute (position);
Show the records that should appear on this page with a for loop
for (int i=1;i<=pagesize;i++) {
%>
<table>
<tr>
<th><%=rs.getstring ("UserName")%></th>
<td> Posted 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%> (total <%=pagecount%> page)
<br>
<a href= "showmessages.jsp?showpage=1" > Home </a>
<a href= "showmessages.jsp?showpage=<%=showpage-1%>" > Prev </a>
<%//Display the number of each page according to the PageCount value 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 </a>
<!--submit the number of pages that the user wants to display through the form-->
<form action= "" method= "get" >
Jump to <input type= "text" name= "ShowPage" size= "4" > page
<input type= "Submit" name= "submit" value= "Jump" >
</form>

The results are as follows (in order to simplify the code, the page layout has been removed from the relevant code, only refers to the function):

You can jump to the first page, Prev, Next, last, you can manually specify the number of pages, you can also enter the input box to display pages.

Related Article

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.