•可以在服务器端的servlet中实现分页,首先在JSP中,通过Java Servlet来检索数据,然后用JSP来调用结果来显示。
•要点:
•将查询条件保存到session中,取session中的查询条件
•设置Statement对象的MaxRows(确定一页显示多少行数据)
•顺序地通过执行SQL语句查询数据,按maxRows来检索一个maxRows的数据,
•下一页再检索下一maxRows的数据,以此类推。
•下面的例子18.2实现了在Servlet中对userinfo表的数据进行分页显示。
•(1)在Servlet目录下,新建一个文件UserListServlet.java,代码如下:
•packagech18.Servlet;
•import java.io.IOException;
•import java.util.Collection;
•
•importjavax.servlet.ServletException;
•importjavax.servlet.http.HttpServletRequest;
•importjavax.servlet.http.HttpServletResponse;
•
•import ch18.JavaBean.DB_User;
•
•/**
•* 用户列表分页显示UserListServlet类
• */
•publicclass UserListServlet extends javax.servlet.http.HttpServlet implements
• javax.servlet.Servlet {
•
• public UserListServlet() {
• super();
• }
•
• protected void doGet(HttpServletRequestrequest,
• HttpServletResponseresponse) throws ServletException,IOException{
•
• response.setHeader("Pragma","No-cache");
• response.setHeader("Cache-Control","no-cache");
• response.setDateHeader("Expires",0);
•intpagesize = 5;
• String page = request.getParameter("page");
• DB_Userdbuser= new DB_User();
• intcount = dbuser.getCount();// 记录总数
• int totalPageNum= count / pagesize;// 计算总页数
•
• // 如果纪录总数除以每页的显示个数,余数大于0,那么
• // 总页数应该为商+1
• if ((count % pagesize)> 0) {
• totalPageNum+= 1;
• }
• if (page == null) {
• page = "1";
• }
• request.setAttribute("totalPageNum",new Integer(totalPageNum));
• Collection c = dbuser.getAll(pagesize,Integer.parseInt(page));
• request.setAttribute("c",c);
• try {
• request.getRequestDispatcher("userlist.jsp").forward(request,
• response);
• }
• catch (Throwablet) {
• getServletContext().log(t.getMessage());
• }
• }
•}
•(2)新建一个文件userlist.jsp,代码如下:
•<%@ pagecontentType="text/html; charset=gb2312" %>
•<%@ pagepageEncoding="GB2312"%>
•<%@ pagelanguage="java" %>
•
•<%@ pageimport="java.util.Collection,java.util.Iterator"%>
•<%@ pageimport="ch18.JavaBean.DB_Conn"%>
•<%@ pageimport="ch18.JavaBean.DB_User"%>
•<%@ pageimport="ch18.JavaBean.User"%>
•<table align=center border=0width=100% cellspacing=1cellpadding=1>
• <tr>
• <tdwidth=100% colspan=5bgcolor=#3399ff>
• <center><font color=#ffffff>成员列表</font></center>
• </td>
• </tr>
• <tr>
• <tdwidth=10% bgcolor=#fcfcfc>用户名</td>
• <tdwidth=10% bgcolor=#fcfcfc>登录密码</td>
• <tdwidth=15% bgcolor=#fcfcfc>邮件地址</td>
• </tr>
• <%
• if (request.getAttribute("c")== null) {
• %>
• <tr>
• <td width= 75% colspan=5bgcolor=#fcfcfcalign=center>没有用户数据!</td>
• </tr>
• <% } else {
• Collection c = (Collection) request.getAttribute("c");
• if(c == null) {
• %>
• <tr>
• <td width=100% colspan=5bgcolor=#fcfcfcalign=center>没有用户数据!</td>
• </tr>
•<%}else {
• inttotalPageNum= ((Integer) (request.getAttribute("totalPageNum"))).intValue();
• intcurrentPage= 1;
• if (request.getParameter("page")!= null) {
• currentPage= Integer.parseInt(request.getParameter("page"));
• }
• //输出用户数据
• Iterator iterator = c.iterator();
• while(iterator.hasNext()) {
• User user = (User) iterator.next();
• %>
•<tr>
• <tdwidth=10% bgcolor=#fcfcfc><%=user.getUsername()%></a></td>
• <tdwidth=15% bgcolor=#fcfcfc><%=user.getPassword()%></td>
• <tdwidth=15% bgcolor=#fcfcfc><%=user.getEmail()%></td>
• </tr>
• <%}%>
•
• <formname="list" method="GET">
• <tr>
• <tdwidth=100% colspan=5align=right bgcolor=#efefef>
• <%for(inti= 1; i<= totalPageNum; i++) {%>
• <a href="UserListServlet?page=<%=i %>">[<%=i%>]</a>
• <%} %>共[<%=request.getAttribute("totalPageNum") %>]页
• <inputtype="text" size="2" name="page"value="">
• <inputtype="submit" name="go" value="Go">
• </td>
• </tr>
• </form>
• <%}
• }
• %>
• <tr>
• <td colspan=2align=right> </td>
• </tr>
•
•</table>
•(3)在web.xml文件中输入以下配置代码。
•<?xmlversion="1.0" encoding="UTF-8" ?>
•<web-appid="WebApp_ID"version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
• xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
• xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
• http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
• <display-name>Login-Register-UserManager</display-name>
• <servlet>
• <description />
• <display-name>PageServlet</display-name>
• <servlet-name>PageServlet</servlet-name>
• <servlet-class>ch18.Servlet.UserListServlet</servlet-class>
• </servlet>
• <servlet-mapping>
• <servlet-name>PageServlet</servlet-name>
• <url-pattern>/UserListServlet</url-pattern>
• </servlet-mapping>
•
•</web-app>