如何使用 servlet jsp写分页

 我来答
huanglenzhi
2015-06-13 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117538 获赞数:517166
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部
  今天给同学用JSP+JavaBean+Servlet做了个分页。用的是MySQL+Tomcat+MyEclispe环境。
  分页计数器如下:
  package org.pagedemo.page;

  /**
  * Create on 2008-08-28 11:05
  *
  * @author qpy_2006
  * 要用到分页,则在创建该类对象时
  * 只需在传入rows的前提下调用counter(page,rows)方法就可以设置firstRow,totalRows,totalPages
  * 在查询的dao中,把该类对象做为参数传入,
  * 再通过调用对象的getFirstRow(),及getMaxSize()设置
  */
  public class PageUtil {

  private int totalRows; // 总行数即总记录数
  private int totalPages; // 总页数
  private int currentPage = 1; // 当前页
  private int maxSize = 3; // 每页存放3条记录
  private int firstRow = 0; // 数据库中第一行数据

  /** Creates a new instance of PageCounter */
  public PageUtil() {
  }

  public int getTotalRows() {
  return totalRows;
  }

  public void setTotalRows(int totalRows) {
  this.totalRows = totalRows;
  }

  public int getTotalPages() {
  return totalPages;
  }

  public void setTotalPages(int totalPages) {
  this.totalPages = totalPages;
  }

  public int getCurrentPage() {
  return currentPage;
  }

  public void setCurrentPage(int currentPage) {
  this.currentPage = currentPage;
  }

  public int getMaxSize() {
  return maxSize;
  }

  public void setMaxSize(int maxSize) {
  this.maxSize = maxSize;
  }

  public int getFirstRow() {
  return firstRow;
  }

  public void setFirstRow(int firstRow) {
  this.firstRow = firstRow;
  }

  // 根据请求参数pageType翻页,即设置firstRow
  // 根据请求参数rows总记录数计算总页数
  public void counter(String pageType,int rows) {
  this.totalRows = rows;
  this.totalPages = (this.totalRows + this.maxSize - 1) / this.maxSize;
  System.out.println("counter 中的totalPages的值为 "+this.totalPages+" 页");
  pageType = pageType.toLowerCase();
  if ("first".equals(pageType)) {
  first();
  } else if ("last".equals(pageType)) {
  last();
  } else if ("next".equals(pageType)) {
  next();
  } else if ("previous".equals(pageType)) {
  previous();
  }
  }

  // 首页
  private void first() {
  currentPage = 1;
  firstRow = 0;
  }

  // 上页
  private void previous() {
  if (totalPages == 0) {
  currentPage = 1;
  } else {
  if (currentPage == 1) {
  currentPage = totalPages;
  } else {
  currentPage--;
  }
  }
  firstRow = (currentPage - 1) * maxSize;
  }

  // 下页
  private void next() {
  if (totalPages == 0) {
  currentPage = 1;
  } else {
  if (currentPage < totalPages) {
  currentPage++;
  } else if (currentPage == totalPages) {
  currentPage = 1;
  }
  }
  firstRow = (currentPage - 1) * maxSize;
  }

  // 尾页
  private void last() {
  currentPage = totalPages;
  if (totalPages == 0) {
  currentPage = 1;
  }
  firstRow = (currentPage - 1) * maxSize;
  }
  }
  查询出所有的记录数:
  package org.pagedemo.page;

  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import org.pagedemo.db.DBConnection;

  public class PageDAO extends DBConnection {

  // 查出所有的记录数
  public int getRows() {
  Connection conn = this.getConnection();
  String sql = "select count(*) from t_page";
  int rows = 0;
  try {
  PreparedStatement pstmt = conn.prepareStatement(sql);
  ResultSet rs = pstmt.executeQuery();
  while (rs.next()) {
  rows = rs.getInt(1);
  }
  System.out.println("rows " + rows);
  } catch (SQLException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  } finally {
  if (conn != null)
  try {
  conn.close();
  } catch (SQLException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
  }
  return rows;
  }
  }

  Servlet中控制分页
  package org.pagedemo.servlet;

  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;

  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;

  import org.pagedemo.page.PageDAO;
  import org.pagedemo.page.PageUtil;
  import org.pagedemo.po.User;
  import org.pagedemo.user.dao.UserDAO;

  /**
  * @author qpy_2006 Servlet,本系统的核心控制器
  */
  public class UserServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;

  public void destroy() {
  super.destroy();

  }

  public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  response.setContentType("text/html");
  this.doPost(request, response);
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  response.setContentType("text/html");
  // 获得HttpSession并取得session,用户保存数据
  // 以便在jsp页面或者其他servlet中获得该session中得值
  HttpSession session = request.getSession();
  // 获取分页的请求类别
  String pageType = request.getParameter("pageType");
  String action = request.getParameter("action");
  List<User> users = new ArrayList<User>();
  PageDAO pd = new PageDAO();
  PageUtil pu = new PageUtil();
  UserDAO ud = new UserDAO();
  // 获取数据库中记录数
  int rows = pd.getRows();
  if ("initial".equals(action)) {
  // 第一次进入index.jsp页面时,查询出第一页
  // 设置分页的各个属性,参见PageUtil这个类
  pu.counter("first", rows);
  session.setAttribute("pageBean", pu);
  } else if ("page".equals(action)) {
  // 点击分页请求时执行下一代码
  pu = (PageUtil) session.getAttribute("pageBean");
  pu.counter(pageType, pu.getTotalRows());
  }
  users = ud.findAll(pu);
  // 把查询的结果users放到session中,
  // 以便在jsp页面从session读取该users对象
  session.setAttribute("users", users);

  // 把PageUtil放到session中
  session.setAttribute("pageBean", pu);
  // 在servlet跳转到某个jsp页面
  request.getRequestDispatcher("index.jsp").forward(request, response);
  }

  public void init() throws ServletException {

  }

  }

  详细代码及工程到如下网址下载:http://download.csdn.net/user/qpyong
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式