In the past few days, we have encountered a problem in the design of JSP message boards. First read one:
This is a picture taken randomly on a news page. If there are too many messages, the entire page would have to be long and long, which directly causes users to feel troublesome and uncomfortable, to solve this problem, we usually use the paging display method.
To design the page display mode, you usually need to use these basic variables: pageSize (number of records displayed on each page) and pageCount (total number of pages) showPage (page number currently displayed) and recordCount (total number of records) draw a picture for ease of understanding:
If you want to display the page to this effect, in this figure, pageSize = 4, pageCount = 3, showPage = 1, recordCount = 12. The idea is that if you want to display that page, you must first calculate the first record of each page as the number of records in all records, assume that the first record on each page is the position record in the total record, position = (ShowPage-1) × PageSize + 1. For example, if you want to display the first page, you need to calculate the first record in the first page as the first record in the total record; if you want to display the second page, the first record on the second page is the fourth record in the total record. to display the third page, the first record on the first page is the ninth record in the total record.
The core code in JSP is as follows (MySQL is used ):
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 );
// Return a scrollable result set
Rs= SQL .exe cuteQuery ("select * from messageinfo ");
// Move the cursor to the last row
Rs. last ();
// Obtain the row number of the last row
Int recordCount = rs. getRow ();
// Calculate the total number of pages
PageCount = (recordCount % pageSize = 0 )? (LastRow/pageSize) :( lastRow/pageSize + 1 );
// Obtain 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;
}
// To display the showPage page, the cursor should be moved to the position value:
Int position = (showPage-1) * pageSize + 1;
// Set the cursor position
Rs. absolute (position );
// Use the for loop to display records on this page
For (int I = 1; I <= pageSize; I ++ ){
%>
<Table>
<Tr>
<Th> <% = rs. getString ("UserName") %> </th>
<Td> posted on: <% = 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 pages <% = pageCount %>)
<Br>
<A href = "ShowMessages. jsp? ShowPage = 1 "> homepage </a>
<A href = "ShowMessages. jsp? ShowPage = showPage-1 %> "> previous </a>
<% // Display the numbers on each page based on the pageCount value and add 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 that the user wants to display through a form -->
<Form action = "" method = "get">
Jump to the <input type = "text" name = "showPage" size = "4"> page
<Input type = "submit" name = "submit" value = "">
</Form>
The running result is as follows (the webpage layout code has been removed to simplify the Code. This is only a function ):
You can jump to the home page, Previous Page, next page, and last page. You can manually specify the number of pages, or enter the number of pages to be displayed in the input box.