Implementation code for JSP paging display

Source: Internet
Author: User
Tags prev

This article mainly introduces the JSP paging display implementation code, the need for friends can refer to

in the past few days in doing JSP message board design process, encountered a problem.        First look at a screenshot: This is random in a news message page cut the map, if the number of message bar is too many, that the whole page has to line up long, this directly to the user caused trouble, uncomfortable feelings, so, to solve this problem, usually 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, drew 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 (using the database for MySQL):     Code is 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 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 total number of 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; & nbsp }   if (showpage>=pagecount) {   showPage=pageCount;  }    //If you want to display page showpage, The value of the position to which the cursor should be moved is:   int position= (showPage-1) *pagesize+1;  //Set cursor position   Rs.absolute (position);  //For loop displays the records that should be displayed on this page   for (int i=1;i<=pagesize;i++) {   %>    <table>     <tr>       <th><%=rs.getstring ("UserName")%></th>   & nbsp  <td> Posted in: <%=rs.geTstring ("datetime")%></td>     </tr>     <TR >      <th Colspa N= "3" ><textarea><%=rs.getstring ("content")%></textarea></th>     </tr>    </table>    <%      rs.next ();  }    Rs.close ();   Con.close ();  }   catch (Exception e) {  e.printstacktrace ();}  %>  <br>   <%=showpage% > page (Total <%=pagecount%> page)  <br>  <a href= "showmessages.jsp?showpage=1" > Home </a>  <a href= "showmessages.jsp?showpage=<%=showpage-1%>" > Prev </a> <%// Displays the number of each page according to the value of the PageCount plus 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>      Running results are as follows (in order to simplify the code, has been page layout related code removed, only refers to the function):   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.