servlet分页代码示例(3)

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * Created by lx_sunwei on 14-1-6.
 */
public class Servlet extends HttpServlet {

public int rowsPerPage;  //每页显示的行数
    public int curPage;  //当前页页码
    public int maxPage;  //总共页数
    DBHelper db = new DBHelper();
    public Servlet(){
        rowsPerPage = 5;
    }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String curPage1 = request.getParameter("page");  //获取当前页页码
        if (curPage1 == null){
            curPage = 1;
            request.setAttribute("curPage",curPage);  //设置curPage对象
        }else {
            curPage = Integer.parseInt(curPage1);
            if (curPage < 1){
                curPage = 1;
            }
            request.setAttribute("curPage",curPage);
        }

List<UserData> dataList;
        dataList = db.getData(curPage,rowsPerPage);  //获取当前页的数据
        maxPage = db.getMaxPage(rowsPerPage);  //获取总页数
        request.setAttribute("dataList",dataList);
        request.setAttribute("maxPage", maxPage);

RequestDispatcher rd = request.getRequestDispatcher("pagemain.jsp");  //将请求转发到pagemain.jsp页面
        rd.forward(request,response);
    }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

4.创建 JSP 页面,显示数据。

复制代码 代码如下:


<%@ page import="java.util.List" %>
<%@ page import="com.tool.UserData" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>servlet数据分页</title>
    <link type="text/css" href="https://www.jb51.net/css.css">
</head>
<body>
<div>
    <table>
        <caption>SCOTT用户,EMP表中的数据</caption>
        <%! int curPage,maxPage; %>
        <% curPage =Integer.parseInt(request.getAttribute("curPage").toString()); %> <!--取得当前页-->
        <% maxPage =Integer.parseInt((String)request.getAttribute("maxPage").toString()); %> <!--取得总页数-->
        <%if (request.getAttribute("dataList") == null){
        %>
        <tr>
            <td colspan="8">没有数据</td>
        </tr>
        <%
        }else {
        %>
        <tr>
            <!--表头-->
            <th>EMPNO</th>
            <th>ENAME</th>
            <th>JOB</th>
            <th>MGR</th>
            <th>HIREDATE</th>
            <th>SAL</th>
            <th>COMM</th>
            <th>DEPTNO</th>
        </tr>
        <%
            List list = (List) request.getAttribute("dataList");
            for (Object aList : list) {
                UserData userData = (UserData) aList;
        %>
        <tr>
            <!--取得表中数据-->
            <td><%= userData.getEmpno() %></td>
            <td><%= userData.getEname() %></td>
            <td><%= userData.getJob() %></td>
            <td><%= userData.getMgr() %></td>
            <td><%= userData.getHireDate() %></td>
            <td><%= userData.getSal() %></td>
            <td><%= userData.getComm() %></td>
            <td><%= userData.getDeptno() %></td>
        </tr>
        <%
                }
            }
        %>
    </table>
</div>
<div>
    第<%= curPage %>页,共<%= maxPage %>页&nbsp;&nbsp;
    <%if (curPage > 1){
    %>
    <a href="https://www.jb51.net/Servlet?page=1">首页</a>
    <a href="Servlet?page=<%=curPage - 1%>">上一页</a>
    <%
    }else {
    %>
    首页 上一页
    <%
        }%>
    <%if (curPage < maxPage){
    %>&nbsp;&nbsp;
    <a href="Servlet?page=<%=curPage + 1%>">下一页</a>
    <a href="Servlet?page=<%=maxPage %>">尾页</a>
    <%
    }else {
    %>
    下一页 尾页
    <%
        }%>
    &nbsp;&nbsp;转至第&nbsp;<form action="Servlet" method="get">
    <label>
        <select onchange="document.form1.submit()">
            <%for ( int i = 1; i <= maxPage; i++){
                if (i == curPage){
            %>
            <!--当前页页码默认选中-->
            <option selected value="<%= i%>"><%= i %></option>
            <%
            }else {
            %>
            <option value="<%= i %>"><%= i %></option>
            <%
                    }
                }%>
        </select>
    </label>
</form>&nbsp;页
</div>
</body>
</html>

web.xml 中的配置文件为:

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzfdgz.html