欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

servlet分页代码示例

程序员文章站 2023-11-17 23:12:10
1.首先创建一个对象 userdata,用以保存从数据库中获取的数据。 复制代码 代码如下:package com.tool; import java.math.big...

1.首先创建一个对象 userdata,用以保存从数据库中获取的数据。

复制代码 代码如下:

package com.tool;

import java.math.bigdecimal;
import java.util.date;

/**
 * created by lx_sunwei on 14-1-6.
 */
public class userdata {

    /**
     * emp表中的数据属性
     */
    private string ename;
    private string job;
    private bigdecimal empno;
    private bigdecimal mgr;
    private date hiredate;
    private bigdecimal sal;
    private bigdecimal comm;
    private bigdecimal deptno;

    public bigdecimal getempno() {
        return empno;
    }

    public void setempno(bigdecimal empno) {
        this.empno = empno;
    }

    public bigdecimal getmgr() {
        return mgr;
    }

    public void setmgr(bigdecimal mgr) {
        this.mgr = mgr;
    }

    public date gethiredate() {
        return hiredate;
    }

    public void sethiredate(date hiredate) {
        this.hiredate = hiredate;
    }

    public bigdecimal getsal() {
        return sal;
    }

    public void setsal(bigdecimal sal) {
        this.sal = sal;
    }

    public bigdecimal getcomm() {
        return comm;
    }

    public void setcomm(bigdecimal comm) {
        this.comm = comm;
    }

    public bigdecimal getdeptno() {
        return deptno;
    }

    public void setdeptno(bigdecimal deptno) {
        this.deptno = deptno;
    }

    public string getename() {
        return ename;
    }

    public void setename(string ename) {
        this.ename = ename;
    }

    public string getjob() {
        return job;
    }

    public void setjob(string job) {
        this.job = job;
    }
}

2.创建一个 dbhelper 对象用以与数据库进行交互

复制代码 代码如下:

package com.dao;

import com.tool.userdata;

import java.math.bigdecimal;
import java.sql.*;
import java.util.*;
import java.util.date;

/**
 * created by lx_sunwei on 14-1-6.
 */
public class dbhelper {

    connection conn;  //数据库连接对象
    preparedstatement pt;  //sql语句预处理对象
    resultset rs;  //结果集对象

    public  dbhelper(){
        try {
            class.forname("oracle.jdbc.driver.oracledriver");  //装载驱动
        } catch (classnotfoundexception e) {
            e.printstacktrace();
        }
    }

    /**
     * 获取当前页的数据
     * @param curpage
     * @param rowsperpage
     * @return
     */
    public list<userdata> getdata(int curpage, int rowsperpage) {

        list<userdata> datalist = new arraylist<>();
        string url = "jdbc:oracle:thin:@localhost:1521:orcl";
        try {
            conn = drivermanager.getconnection(url,"scott","tiger");
            string sql = "select * from emp where rownum <= ((? - 1) * "+rowsperpage+" + "+rowsperpage+") minus " +
                    " select * from emp where rownum <= (? - 1) * "+rowsperpage+" ";
            pt = conn.preparestatement(sql);
            pt.setint(1,curpage);
            pt.setint(2,curpage);
            rs = pt.executequery();
            while (rs.next()){
                /**
                 * 从结果集中取得数据
                 */
                userdata userdata = new userdata();
                bigdecimal empno = rs.getbigdecimal("empno");
                string ename = rs.getstring("ename");
                string job = rs.getstring("job");
                bigdecimal mgr = rs.getbigdecimal("mgr");
                date hiredate = rs.getdate("hiredate");
                bigdecimal sal = rs.getbigdecimal("sal");
                bigdecimal comm = rs.getbigdecimal("comm");
                bigdecimal deptno = rs.getbigdecimal("deptno");
                /**
                 * 设置对象属性
                 */
                userdata.setempno(empno);
                userdata.setename(ename);
                userdata.setjob(job);
                userdata.setmgr(mgr);
                userdata.sethiredate(hiredate);
                userdata.setsal(sal);
                userdata.setcomm(comm);
                userdata.setdeptno(deptno);
                datalist.add(userdata);  //把对象添加集合中
            }
            rs.close();
            pt.close();
            conn.close();
        } catch (sqlexception e) {
            e.printstacktrace();
        }
        return datalist;
    }

    /**
     * 返回总页数
     * @return
     */
    public int getmaxpage(int rowsperpage) {
        int maxpage;
        int maxrowcount = 0;
        string url = "jdbc:oracle:thin:@localhost:1521:orcl";
        try {
            conn = drivermanager.getconnection(url,"scott","tiger");  //创建数据库连接
            string sql = "select count(*) from emp";
            pt = conn.preparestatement(sql);
            rs = pt.executequery();
            if (rs.next()){
                maxrowcount = rs.getint(1);  //总行数
            }
        } catch (sqlexception e) {
            e.printstacktrace();
        }
        maxpage = (maxrowcount + rowsperpage - 1) / rowsperpage;  //总页数
        return maxpage;
    }
}

3.创建 servlet 对显示页面进行控制

复制代码 代码如下:

package com.servlet;

import com.dao.dbhelper;
import com.tool.userdata;

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 rel="stylesheet" type="text/css" href="css.css">
</head>
<body>
<div style="margin-top: 15%; margin-left: 25%">
    <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 style="margin-top: 8%; margin-left: 29%">
    第<%= curpage %>页,共<%= maxpage %>页  
    <%if (curpage > 1){
    %>
    <a href="servlet?page=1">首页</a>
    <a href="servlet?page=<%=curpage - 1%>">上一页</a>
    <%
    }else {
    %>
    首页 上一页
    <%
        }%>
    <%if (curpage < maxpage){
    %>  
    <a href="servlet?page=<%=curpage + 1%>">下一页</a>
    <a href="servlet?page=<%=maxpage %>">尾页</a>
    <%
    }else {
    %>
    下一页 尾页
    <%
        }%>
      转至第 <form name="form1" action="servlet" method="get">
    <label>
        <select name="page" 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> 页
</div>
</body>
</html>

web.xml 中的配置文件为:

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
           xsi:schemalocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0">

    <servlet>
        <servlet-name>servlet</servlet-name>
        <servlet-class>com.servlet.servlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>servlet</servlet-name>
        <url-pattern>/servlet</url-pattern>
    </servlet-mapping>

</web-app>

把项目部署到 tomcat 服务器上,输入地址:http://localhost:8080/servlet   这样就可以看到效果