使用servlet编写如下页面:
下面是分页技术的详解:
思路:
定义四个分页变量
pageNow 表示第几页,该变量是由用户来决定,因此变化
pageSize 每页显示几条记录,由程序指定,也可以由用户定制
pageCount 表示共有多少页, 该变量是计算出来->思考 怎样确定
rowCount 共有多少条记录,该变量是查询数据库得到
如何确定pageCount
(1)
if(rowCount% pageSize==0){
pageCount=rowCount/pageSize;
}else{
pageCount= rowCount/pageSize+1;
}
试试: 比如 users表 9 条记录 pageSize=3 =>pageCount=3
比如 users表 10 条记录 pageSize=3 =>pageCount=4
(2)
上面的算法等价于
pageCount=rowCount% pageSize==0 ? rowCount/pageSize: rowCount/pageSize+1;
该运算称为三目运算
(3)
更简单的算法是:
pageCount=(rowCount-1)/pageSize+1;
试试: 比如 users表 9 条记录 pageSize=3 =>pageCount=3
比如 users表 11 条记录 pageSize=3 =>pageCount=4
对应的Servlet代码为:
package com.dengqi.view;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dengqi.util.JDBCUtils;
public class ManageServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<script type='text/javascript' language='javascript'>");
out.println("function gotoPageNow(){var pageNow=document.getElementById('pageNow');window.alert('pageNow:' + pageNow.value)");
out.println("</script>");
out.println("<h1>管理用户</h1>");
JDBCUtils jdbcUtils = new JDBCUtils();
int pageNow = 1;
String sPageNow = request.getParameter("pageNow");
if(null != sPageNow)
{
pageNow = Integer.parseInt(sPageNow);
}
int pageSize = 3;
int pageCount = 1;
int rowCount = 1;
rowCount = jdbcUtils.queryRowsize();
pageCount= rowCount%pageSize == 0 ? rowCount/pageSize : rowCount/pageSize + 1;
String querySql = "select * from users limit " + (pageNow-1)*pageSize + "," + pageSize;
try {
ResultSet resultSet = jdbcUtils.query(querySql, null);
out.println("<table border=1 width=500px bordercolor=green cellspacing=0>");
out.println("<tr><th>用户id</th><th>用户名</th><th>邮箱</th><th>级别</th></tr>");
while(resultSet.next())
{
out.println(
"<tr><td>"+resultSet.getInt(1)+
"</td><td>"+resultSet.getString(2)+
"</td><td>"+resultSet.getString(4)+
"</td><td>"+resultSet.getInt(5)+
"</td></tr>");
}
out.println("</table><br/>");
int forwordPage = 1;
if(pageNow != 1)
{
forwordPage = pageNow - 1;
}
out.println("<a href='/UsersManager/ManageServlet?pageNow=" + forwordPage + "'><上一页></a>");
for(int i =1; i <= pageCount; i ++)
{
out.println("<a href='/UsersManager/ManageServlet?pageNow=" + i +"'><" + i + "></a>");
}
int nextPage = pageCount;
if(pageNow != pageCount)
{
nextPage = pageNow + 1;
}
out.println("<a href='/UsersManager/ManageServlet?pageNow=" + nextPage + "'><下一页></a>");
out.println("  当前页:" + pageNow + "/总页数:" + pageCount + "<br/>");
out.println("跳转到第" + "<input type='text' id='pageNow' name='pageNow' size='20px'/>页");
out.println("<input type='button' value='确定' onclick='gotoPageNow()'/>");
out.println("<br/>");
out.println("<br/>");
out.println("<a href='/UsersManager/MainFrame'>返回主界面</a>");
out.println("<a href='/UsersManager/LoginServlet'>安全退出</a>");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
jdbcUtils.releaseConn();
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}