码迷,mamicode.com
首页 > 编程语言 > 详细

java web 分页技术

时间:2016-03-24 14:48:15      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

查询界面,通过姓名和车号进行查询,然后将查询结果提交给servlet:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body background="images/bottom.png">
<br><br><br><br><br>
   <form action="<%=request.getContextPath()%>/SessionServrlet" method="get">
       <table boder="1" align="center">
         <tr>
           <td align="right"><span style="color:white">姓名:</span></td>
           <td><input type="text" name="name" size="30" /></td>
         </tr>
         <tr>
           <td align="right"><span style="color:white">车号:</span></td>
           <td><input type="text" name="carNum" size="30" /></td>
         </tr>
          <tr>
           <td><input type="hidden" name="URL" value="beginQueryDynamicMonitor.jsp"></td>
         </tr>
         <tr align="right">
           <td colspan="2">
             <input type="submit" name="submit" value="查询">
             <input type="reset" name="reset" value="重置">
           </td>
         </tr>
       </table>
   </form>
</body>
</html>

在servlet将从页面获取到的姓名和车号存到session中(因为request有效期只是一次请求的全过程,翻页的时候就会失效,所以要将其存到session中)

package shouqisystem.user;

import java.io.IOException;

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

/**
 * Servlet implementation class ServiceServlet
 */
public class ServiceServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServiceServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
         String name=new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");
         String carNum=new String(request.getParameter("carNum").getBytes("iso-8859-1"),"utf-8");
         String s1="beginQueryService.jsp";
         HttpSession session = request.getSession();
         session.setAttribute("name", name);
         session.setAttribute("carNum", carNum);
         response.setContentType("text/html;charset=UTF-8");
         String s = response.encodeURL(s1);
         RequestDispatcher disp=null;
         disp=request.getRequestDispatcher(s);
         disp.forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

Pages.java控制分页

package shouqisystem.user;

import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;

public class Pages {
        ResultSet CountTopicrs=null;//记录总数结果集
        ResultSet Pagirs=null;//查询结果集
        public int intCountTopic=0;//记录总数
        public int intPageSize;//每页显示几条记录
        public int intPageCount;//总页数
        public int intPage=1;//当前页数
        public String nowPage;
        public String HttpFile;
        UserInfo db;
        
        public Pages(){
            intPageSize=2;
            db=new UserInfo();
        }
        
        public void querySql(ResultSet CountTopicrs,ResultSet Pagirs,HttpServletRequest request)throws Exception{
            HttpFile=request.getRequestURI();
            nowPage=request.getParameter("pages");
            if(nowPage==null){
                intPage=1;
            }else{
                intPage=Integer.parseInt(nowPage);
                if(intPage<1)
                    intPage=1;
            }
            
            //CountTopicrs=db.queryICcardDataSum(name, carNum);
            if(CountTopicrs.next()){
                intCountTopic=CountTopicrs.getInt(1);
            }
            intPageCount=(intCountTopic+intPageSize-1)/intPageSize;
            if(intPage>intPageCount){
                intPage=intPageCount;
            }
            CountTopicrs.close();
            //db.close_all();
            //Pagirs=db.queryICcardData(name, carNum);
            //return Pagirs;
        }
        public int getCountTopic(){
            return intCountTopic;
        }
        public int getPageCount(){
            return intPageCount;
        }
        public int getIntPage(){
            return intPage;
        }
        public String PageFooter(){
            String str="";
            int next,prev;
            prev=intPage-1;
            next=intPage+1;
            System.out.println();
            str+="<font color=red>查询到"+getCountTopic()+"条记录</font>"+" <font color=red> 共"+getPageCount()+"页</font>";
            str+="<font color=red>第"+getIntPage()+"页</font>";
            if(intPage>1)
                str+="<font color=red><a href=" + HttpFile + "?pages=1"+">首页</a></font>";
            else 
                str+="<font color=red>首页";
            if(intPage>1)
                str +="<font color=red><a href=" + HttpFile + "?pages="+prev+">上一页</a></font>";
            else
                str +="<font color=red>上一页</font>";
            if(intPage<intPageCount)
                str +="<font color=red><a href=" + HttpFile + "?pages="+next+">下一页</a></font>";
            else
                str +="<font color=red>下一页</font>";
            if(intPageCount>1&&intPage !=intPageCount)
                str +="<font color=red><a href=" + HttpFile + "?pages="+intPageCount+">尾页</a></font>";
            else
                str +="<font color=red>尾页</font>";
            return str;
        }
        public void close_all()throws Exception{
            db.close_all();
        }
}

查询结果页面:

<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.io.*,java.util.*,java.sql.*,shouqisystem.user.*" %>
<jsp:useBean id="userinfo" scope="request" class="shouqisystem.user.UserInfo"/>
<jsp:useBean id="pages" scope="page" class="shouqisystem.user.Pages"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body background="images/bottom.png">
<center>
   <form action="" method="GET" name="form">
  <table boder="2" cellpadding="5" cellspacing="5">
  <tr>
     <td><b><span style="color:white">选择</span></b></td>
     <td><b><span style="color:white">姓名</span></b></td>
     <td><b><span style="color:white">车牌号</span></b></td>
     <td><b><span style="color:white">超速日期</span></b></td>
     <td><b><span style="color:white">超速四级次数</span></b></td>
     <td><b><span style="color:white">出外埠日期</span></b></td>
     <td><b><span style="color:white">去向</span></b></td>
     <td><b><span style="color:white">疲劳驾驶日期</span></b></td>
     <td><b><span style="color:white">级别</span></b></td>
     <td><b><span style="color:white">次数</span></b></td>
  </tr>
  <%

  String name=(String)session.getAttribute("name");
  String carNum=(String)session.getAttribute("carNum");
  ResultSet CountTopicrs=userinfo.queryDynamicMonitorSum(name, carNum);
  ResultSet rs=userinfo.queryDynamicMonitor(name, carNum);
  pages.querySql(CountTopicrs,rs,request);
  String footer=pages.PageFooter();
  if(pages.intPageCount>0){
      int i=0;
      while(rs.next())
      {
          i++;
          if(i>((pages.intPage-1)*pages.intPageSize)&&(i<=pages.intPage*pages.intPageSize))
          {  
  %>
  <tr>
 
      <td><input type="checkbox" name="pkey" size="5" value="<%=String.valueOf(rs.getInt("ID")) %>"/></td>
      <td><span style="color:white"><%=rs.getString("name") %></span></td>
      <td><span style="color:white"><%=rs.getString("carNum") %></span></td>
      <td><span style="color:white"><%=rs.getString("speedTime") %></span></td>
      <td><span style="color:white"><%=rs.getString("speedFourLevelTimes") %></span></td>
      <td><span style="color:white"><%=rs.getString("outGoingTime") %></span></td>
      <td><span style="color:white"><%=rs.getString("destination") %></span></td>
      <td><span style="color:white"><%=rs.getString("fatigueDrivingTime") %></span></td>
      <td><span style="color:white"><%=rs.getString("degree") %></span></td>
      <td><span style="color:white"><%=rs.getString("times") %></span></td>
  </tr>
  <%    
  }  
      }
  }
  out.println("<tr><td colspan=2>"+footer+"</td></tr>");
  rs.close();
  pages.close_all();
   %>
   <tr align="right">
           <td colspan="2">
             <input type="submit" name="revise" value="修改" onclick="form.action=‘reviseDynamicMonitor.jsp‘;form.submit();">
             <input type="submit" name="delete" value="删除" onclick="form.action=‘deleteDynamicMonitor.jsp‘;form.submit();">
           </td>
   </tr>

   </table><br><br> 
   </form>

   </center>
</body>
</html>

 

java web 分页技术

标签:

原文地址:http://www.cnblogs.com/honeyshuo/p/5315378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!