Servlet achieves paging effect, servlet implements Paging

Source: Internet
Author: User

Servlet achieves paging effect, servlet implements Paging

The examples in this article share the code for implementing the paging Effect of Servlet for your reference. The details are as follows:

Paging algorithm:

Four variables need to be defined for their respective use
Int pageSize: How many records are displayed on each page
Int pageNow: page number to be displayed
Int pageCount: Total number of pages
Int rowCount: Total number of records

Note:

PageSize is specified, and pageNow is the user's choice.
RowCount is obtained from the table query.
PageCount is calculated. The formula is as follows:

if(rowCount%pageSize==0) {  pageCount=rowCount/pageSize; } else {  pageCount=rowCount/pageSize+1; } 

If you use the statement: select field name list from table name where id? And?
This SQL statement is indeed fast, but there is a problem, that is, if the table id is deleted, a page may have one fewer record.

Therefore, the final method is as follows:
Select top pageSize field name list from table name where id not in (select top pageSize * (pageNow-1) id from table name)

Implementation Code:

Import javax. servlet. http. *; import java. io. *; import java. SQL. *; public class fenye extends HttpServlet {public void doGet (HttpServletRequest req, HttpServletResponse res) {Connection ct = null; PreparedStatement ps = null; ResultSet rs = null; int pageSize = 3; // The number of records that you want to display on each page int pageNow = 1; // initialize the current page as the first page int pageCount = 0; // the total number of pages, you need to know through calculation that int rowCount = 0; // The total number of records, the query table knows String sPageNow = req. getParameter ("pageNow "); // Receive the passed current page if (sPageNow! = Null) // if a non-null value is received, convert it to an Integer {pageNow = Integer. parseInt (sPageNow);} try {PrintWriter pw = res. getWriter (); Class. forName ("com. microsoft. sqlserver. jdbc. SQLServerDriver "); ct = DriverManager. getConnection ("jdbc: sqlserver: // 127.0.0.1: 1433; DatabaseName = Students", "sa", "password"); ps = ct. prepareStatement ("select count (*) from [Students]. [dbo]. [Students] "); // obtain the total number of records in the table rs1_ps.exe cuteQuery (); while (rs. next () {rowCount = r S. getInt (1); // obtain the total number of records in the table} if (rowCount % pageSize = 0) // calculate the total number of pages {pageCount = rowCount/pageSize ;} else {pageCount = rowCount/pageSize + 1;} ps = ct. prepareStatement ("select top" + pageSize + "* from [Students]. [dbo]. [Students] where id not in (select top "+ pageSize * (pageNow-1) +" id from [Students]. [dbo]. [Students]) "); rs1_ps.exe cuteQuery (); pw. println ("<body> <center>"); // displays the query result in the form of a table pw. println ("<table border = 1 "); pw. println ("<tr> <th> id </th> <th> name </th> <th> grade </th> </tr> "); while (rs. next () {pw. println ("<tr>"); pw. println ("<td>" + rs. getInt (1) + "</td>"); pw. println ("<td>" + rs. getString (2) + "</td>"); pw. println ("<td>" + rs. getString (3) + "</td>"); pw. println ("</tr>");} pw. println ("</table>"); if (pageNow = 1) // the previous page hyperlink. When you have already jumped to the first page, the page will not change {pw. println ("<a href = fenye? PageNow = "+ pageNow +"> "+" forward "+" </a> ");} When else // does not jump to the first page, each time you click a hyperlink, page jump forward once {pw. println ("<a href = fenye? PageNow = "+ (pageNow-1) +"> "+" forward "+" </a> ");} if (pageCount <= 5) // control the number of page hyperlinks displayed {for (int I = 1; I <= pageCount; I ++) {pw. println ("<a href = fenye? PageNow = "+ I +"> "+ I +" </a> ") ;}} else if (pageCount-pageNow <= 5) {for (int I = pageNow; I <= pageCount; I ++) pw. println ("<a href = fenye? PageNow = "+ I +"> "+ I +" </a> ");} else // when there are too many pages, for better page appearance, you need to control the number of display hyperlinks {for (int I = pageNow; I <= pageNow + 5; I ++) pw. println ("<a href = fenye? PageNow = "+ I +"> "+ I +" </a> ");} if (pageNow = pageCount) // when the last page is displayed, {pw. println ("<a href = fenye? PageNow = "+ pageNow +"> "+" backward "+" </a> ");} else {pw. println (" <a href = fenye? PageNow = "+ (pageNow + 1) +"> "+" backward "+" </a> ");} pw. println ("</center> </body>");} catch (Exception ex) {ex. printStackTrace () ;}} public void doPost (HttpServletRequest req, HttpServletResponse res) {this. doGet (req, res );}}

Execution result:

When the number of records per page is 3:

Click the corresponding connection to jump.

The last page is displayed as follows:

Code:

if(pageCount<=5)   {     for(int i=1;i<=pageCount;i++)      {     pw.println("<a href=fenye?pageNow="+i+">"+i+"</a>");      }   } 

Click backward.

To show how the program controls the number of page hyperlinks, change the number of records displayed on each page to 1.

The first page shows the effect:

Code:

else     {       for(int i=pageNow;i<=pageNow+5;i++)         pw.println("<a href=fenye?pageNow="+i+">"+i+"</a>");     } 

Display effect when the current page number increases gradually:

 

Code:

 else if(pageCount-pageNow<=5)     {       for(int i=pageNow;i<=pageCount;i++)       pw.println("<a href=fenye?pageNow="+i+">"+i+"</a>");     } 

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.