使用Servlet完成分页

要将大量记录分为多个部分,我们可以使用分页展示。每一页只是显示部分记录。因为在单个页面中加载所有记录可能需要一些时间,所以我们建议数据多的时候都需要创建分页。在Servlet中,我们可以轻松开发分页示例。

在下面的Servlet分页示例中,我们使用MySQL数据库来获取记录。

1 创建表和准备数据

参考之前的文章:《使用Servlet完成CRUD》

2 编写实体类

package com.yiidian.domain;

/**
 *一点教程网 - http://www.yiidian.com
 */
public class Customer {
    private Integer id;
    private String name;
    private String gender;
    private String telephone;
    private String address;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

3 编写主页

index.jsp,用于进入分页列表

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>一点教程网-使用Servlet分页展示数据</title>
</head>
<body>
<h1>使用Servlet分页展示数据</h1>
<a href="ViewServlet">查询客户</a>
</body>
</html>

4 编写CustomerDao

CustomerDao提供数据库分页查询方法:

package com.yiidian.dao;

import com.yiidian.domain.Customer;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class CustomerDao {
    public static Connection getConnection(){
        Connection con=null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            con= DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
        }catch(Exception e){System.out.println(e);}
        return con;
    }

    public static List<Customer> getRecords(int start,int total){
        List<Customer> list=new ArrayList<Customer>();
        try{
            Connection con=getConnection();
            PreparedStatement ps=con.prepareStatement("select * from customer limit "+start+","+total);
            ResultSet rs=ps.executeQuery();
            while(rs.next()){
                Customer e=new Customer();
                e.setId(rs.getInt(1));
                e.setName(rs.getString(2));
                e.setGender(rs.getString(3));
                e.setTelephone(rs.getString(4));
                e.setAddress(rs.getString(5));
                list.add(e);
            }
            con.close();
        }catch(Exception e){
            System.out.println(e);
        }
        return list;
    }

    
}

5 编写ViewServlet

ViewServlet用于分页展示客户列表

package com.yiidian.servlet;

import com.yiidian.dao.CustomerDao;
import com.yiidian.domain.Customer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 一点教程网 - http://www.yiidian.com
 */
@WebServlet("/ViewServlet")
public class ViewServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();

        String pageStr=request.getParameter("page");

        int page = 1;//当前页码
        int size=3;//每页显示记录数

        if(pageStr!=null && !pageStr.equals("")){
            page = Integer.parseInt(pageStr);
        }

        //计算每页起始行
        int start = (page-1)*size;

        List<Customer> list=CustomerDao.getRecords(start,size);

        out.print("<h1>当前页: "+page+"</h1>");
        out.print("<table border='1' cellpadding='4' width='60%'>");
        out.print("<tr><th>编号</th><th>客户名称</th><th>性别</th><th>联系电话</th><th>地址</th>");
        for(Customer e:list){
            out.print("<tr><td>"+e.getId()+"</td><td>"+e.getName()+"</td><td>"+e.getGender()+"</td><td>"+e.getTelephone()+"</td><td>"+e.getAddress()+"</td></tr>");
        }
        out.print("</table>");

        out.print("<a href='ViewServlet?page=1'>1</a> ");
        out.print("<a href='ViewServlet?page=2'>2</a> ");

        out.close();
    }
}

6 运行测试

1)访问主页:http://localhost:8080/,如下:

2)下面是第一页数据,点击2进入第二页

3)第二页数据


本案例需要在lib目录导入mysql驱动程序,点击下面链接下载:

下载mysql-connector-java-5.1.40.jar

热门文章

优秀文章