步骤:
1. 创建SepPage类,并设置有关的方法。
2. 在进行页面的转移时传递并当前的页面并传递参数。
3. 进行分页处理。
a. 计算总记录数
b. 怎么样进行分页
c. 首页显示处理
d. 传递有关参数
e. 获取有关传递的页面的参数
实例:以查询所有的用户信息为例子进行说明
package bean;
/**********************分页类描述************************/
public class SepPage
{
private int allRows; // 一共有多少行记录
private int curPage = 1; // 当前页面
private int rowPerPage = 8; // 一页显示多少行
private int allPages; // 一共有多少页
public int getAllRows()
{
return allRows;
}
public void setAllRows(int allRows)
{
this.allRows = allRows;
}
public int getCurPage()
{
return curPage;
}
public void setCurPage(int curPage)
{
this.curPage = curPage;
}
public int getRowPerPage()
{
return rowPerPage;
}
public void setRowPerPage(int rowPerPage)
{
this.rowPerPage = rowPerPage;
}
public int getAllPages()
{
return allPages;
}
public void setAllPages(int allPages)
{
this.allPages = allPages;
}
}
2.数据接收servlet
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.DBOperationBean;
import java.util.*;
import util.Convert;
import java.sql.*;
import bean.*;
/**
* servlet类,获取有关客户的信息并进行相关的处理
* @author qihuasun
*/
public class CustomerServlet extends HttpServlet
{
//查询所有sql语句
public static final String SELECTBYALL="select * from SCOTT.EXRM_T_CUSTOMER order by CUM_FULLNAME ";
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
//处理有关中文乱码问题
request.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GBK");
response.setCharacterEncoding("GBK");
String status=request.getParameter("status").trim();//获取页面传递的标志位status
String path="error.jsp";
if(!status.equals("")|| status!=null)
{
if(status.equals("findall"))
{
DBOperationBean db=new DBOperationBean();
String page=request.getParameter("curpage");
try
{
int curPage = Integer.parseInt(request.getParameter("curPage"));
//获取页面传递的参数
SepPage pa=new SepPage();
pa.setCurPage(curPage);
ArrayList al=db.query(this.SELECTBYALL,pa);
if(al!=null)
{
// path="main.jsp";
path="TestMain2.jsp";
request.setAttribute("all",al);
}
else
{
path="error.jsp";
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
request.getRequestDispatcher(path).forward(request,response);
}
}
4. 数据访问类
package bean;
import java.sql.*;
import javax.sql.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.naming.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import