This example shares the specific code for servlet to realize the pagination effect for your reference. The specific content is as follows
Pagination algorithm:
Four variables need to be defined, they have their own uses
int pageSize: How many records are displayed per page
int pageNow: Which page I hope to display
int pageCount: How many pages are there in total
int rowCount: How many records are there in total
illustrate:
pageSize is specified, pageNow refers to the user's choice.
rowCount is obtained from the table.
pageCount is calculated, and the calculation formula is:
if(rowCount%pageSize==0) { pageCount=rowCount/pageSize; } else { pageCount=rowCount/pageSize+1; } If using statement: select field name list from table name where id between? and ?
This SQL statement is indeed faster, but there is a problem, that is, if the id of the table is deleted, then a certain page may have one record missing.
Therefore, the final method is the following statement:
select top pageSize field name list from table name where id not in(select top pageSize*(pageNow-1) id from table name)
The implementation code is:
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; //I hope that the number of records will be displayed for each page int pageNow=1; //Initialize the current page to the first page int pageCount=0; //Total number of pages, you need to know int through calculation rowCount=0; //Record the total number and look up the table to find out String sPageNow=req.getParameter("pageNow"); //Receive the passed current page if(sPageNow!=null) //If a non-null value is received, convert it into 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]"); //Get the total number of records in the table rs=ps.executeQuery(); while(rs.next()) { rowCount=rs.getInt(1); //Get 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])"); rs=ps.executeQuery(); pw.println("<body><center>"); //Show the query results 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 hyperlink on the previous page, when it has already jumped to the first page, the page will no longer change { pw.println("<a href=fenye?pageNow="+pageNow+">"+"forward"+"</a>"); } else //When it is not jumped to the first page, every time the hyperlink is clicked, the page will jump forward { pw.println("<a href=fenye?pageNow="+(pageNow-1)+">"+"forward"+"</a>"); } if(pageCount<=5) //Controll the number of pages displayed in the number of hyperlinks { 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 the beautiful page, you need to control the number of hyperlinks displayed { for(int i=pageNow;i<=pageNow+5;i++) pw.println("<a href=fenye?pageNow="+i+">"+i+"</a>"); } if(pageNow==pageCount) // When it is already the last page, clicking the next page will no longer jump { 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 results:
When the number of records displayed per page is 3:
Click the corresponding connection to jump successfully.
The last page is displayed as:
Corresponding code:
if(pageCount<=5) { for(int i=1;i<=pageCount;i++) { pw.println("<a href=fenye?pageNow="+i+">"+i+"</a>"); } }Click backward to no longer jump.
In order to show how effective the program controls the number of pages hyperlinks, change the number of records displayed per page to 1.
The first page displays the effect:
Corresponding code:
else { for(int i=pageNow;i<=pageNow+5;i++) pw.println("<a href=fenye?pageNow="+i+">"+i+"</a>"); }The display effect when the current page number is gradually increasing:
Corresponding 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 to everyone's learning and I hope everyone will support Wulin.com more.