servlet分頁

採用servlet進行分頁處理
Java代碼 複製代碼
步驟:   

1.  創建SepPage類,並設置有關的方法。   

2.  在進行頁面的轉移時傳遞並當前的頁面並傳遞參數。   

3.  進行分頁處理。   

a.  計算總記錄數   

b.  怎麼樣進行分頁   

c.  首頁顯示處理   

d.  傳遞有關參數   

e.  獲取有關傳遞的頁面的參數   

 

  

實例:以查詢所有的用戶信息爲例子進行說明   

  

  1. package bean;   
  2. /**********************分頁類描述************************/  
  3. public class SepPage    
  4. {   
  5.     private int allRows; // 一共有多少行記錄   
  6.     private int curPage = 1// 當前頁面   
  7.     private int rowPerPage = 8// 一頁顯示多少行   
  8.     private int allPages; // 一共有多少頁   
  9.     public int getAllRows()    
  10.     {   
  11.         return allRows;   
  12.     }   
  13.     public void setAllRows(int allRows)    
  14.     {   
  15.         this.allRows = allRows;   
  16.     }   
  17.     public int getCurPage()    
  18.     {   
  19.         return curPage;   
  20.     }   
  21.     public void setCurPage(int curPage)   
  22.     {   
  23.         this.curPage = curPage;   
  24.     }   
  25.     public int getRowPerPage()    
  26.     {   
  27.         return rowPerPage;   
  28.     }   
  29.     public void setRowPerPage(int rowPerPage)    
  30.     {   
  31.         this.rowPerPage = rowPerPage;   
  32.     }   
  33.     public int getAllPages()   
  34.     {   
  35.         return allPages;   
  36.     }   
  37.     public void setAllPages(int allPages)   
  38.     {   
  39.         this.allPages = allPages;   
  40.     }   
  41. }   
  42.   
  43.   
  44. 2.數據接收servlet   
  45. package servlet;   
  46. import java.io.IOException;   
  47. import java.io.PrintWriter;   
  48. import javax.servlet.ServletException;   
  49. import javax.servlet.http.HttpServlet;   
  50. import javax.servlet.http.HttpServletRequest;   
  51. import javax.servlet.http.HttpServletResponse;   
  52. import bean.DBOperationBean;   
  53. import java.util.*;   
  54. import util.Convert;   
  55. import java.sql.*;   
  56. import bean.*;   
  57. /**  
  58.  * servlet類,獲取有關客戶的信息並進行相關的處理  
  59.  * @author qihuasun  
  60.  */  
  61. public class CustomerServlet extends HttpServlet   
  62. {   
  63.     //查詢所有sql語句   
  64.     public static final String SELECTBYALL="select * from SCOTT.EXRM_T_CUSTOMER order by CUM_FULLNAME ";   
  65.     public void doGet(HttpServletRequest request, HttpServletResponse response)   
  66.     throws ServletException, IOException    
  67.     {   
  68.          this.doPost(request, response);   
  69.      }   
  70.     public void doPost(HttpServletRequest request, HttpServletResponse response)   
  71.     throws ServletException, IOException    
  72.     {   
  73.         //處理有關中文亂碼問題   
  74.         request.setCharacterEncoding("GBK");   
  75.         response.setContentType("text/html;charset=GBK");   
  76.         response.setCharacterEncoding("GBK");   
  77.         String status=request.getParameter("status").trim();//獲取頁面傳遞的標誌位status   
  78.         String path="error.jsp";   
  79.          if(!status.equals("")|| status!=null)   
  80.          {   
  81.            
  82.            if(status.equals("findall"))   
  83.           {   
  84.                DBOperationBean db=new DBOperationBean();   
  85.               String page=request.getParameter("curpage");   
  86.                try  
  87.                 {      
  88.                    int curPage = Integer.parseInt(request.getParameter("curPage"));   
  89. //獲取頁面傳遞的參數   
  90.                     SepPage pa=new SepPage();   
  91.                     pa.setCurPage(curPage);   
  92.                     ArrayList al=db.query(this.SELECTBYALL,pa);   
  93.                     if(al!=null)   
  94.                      {   
  95.                         // path="main.jsp";   
  96.                           path="TestMain2.jsp";   
  97.                           request.setAttribute("all",al);   
  98.                      }   
  99.                     else  
  100.                     {   
  101.                         path="error.jsp";   
  102.                     }   
  103.                    
  104.                 }   
  105.                 catch(Exception ex)   
  106.                 {   
  107.                     ex.printStackTrace();   
  108.                 }      
  109.           }   
  110.              
  111.          }   
  112.          request.getRequestDispatcher(path).forward(request,response);   
  113.            
  114.     }   
  115.   
  116. }   
  117.   
  118. 4.  數據訪問類   
  119.  package bean;   
  120. import java.sql.*;   
  121.   
  122. import javax.sql.*;   
  123. import javax.xml.parsers.DocumentBuilder;   
  124. import javax.xml.parsers.DocumentBuilderFactory;   
  125. import javax.naming.*;   
  126. import org.w3c.dom.Document;   
  127. import org.w3c.dom.Element;   
  128. import org.w3c.dom.NodeList;   
  129. import java.util.*;   
  130. import bean.SepPage;   
  131. /**  
  132.  * bean類,獲取有關配置文件的信息的頁面的信息,並進行有關的處理  
  133.  * @author qihuasun  
  134.  */  
  135. public class DBOperationBean    
  136. {   
  137.      //驅動   
  138.    private final String DBDRIVER    = "oracle.jdbc.driver.OracleDriver";   
  139.    // 數據庫連接地址   
  140.    private final String DBURL       = "jdbc:oracle:thin:@127.0.0.1:1521:data";   
  141.     // 數據庫用戶名   
  142.    private final String DBUSER      = "SCOTT";   
  143.     // 數據庫連接密碼   
  144.    private final String DBPASSWORD  = "sqh";   
  145.     // 聲明一個數據庫連接對象   
  146.    private Connection conn              = null ;   
  147.    private PreparedStatement  pstm=null;   
  148.    private ResultSet rs=null;   
  149.    public DBOperationBean()   
  150.    {   
  151.        this.init();   
  152.    }   
  153.     private void init()  //從數據庫連接屬性XML配置文件中獲取關於連接的信息   
  154.     {       
  155.         conn=new DBConnection().getConnection();   
  156.     }   
  157.     private Connection getConnection() //取得數據庫連接並設置爲當前連接   
  158.     {      
  159.          try  
  160.          {   
  161.               Class.forName(DBDRIVER);    
  162.                 
  163.               conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);// 連接數據庫   
  164.               System.out.println("connected");   
  165.          }   
  166.          catch(Exception ex)   
  167.          {   
  168.              ex.printStackTrace();   
  169.          }   
  170.              
  171.            
  172.         return this.conn;   
  173.     }           
  174.        
  175.     public ArrayList query(String sql,SepPage page) throws Exception   
  176.     {//執行查詢,返回結果集   
  177.         ArrayList al=new ArrayList();   
  178.         try  
  179.         {   
  180.             Connection conn=this.getConnection();   
  181.             pstm = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,   
  182.                     ResultSet.CONCUR_READ_ONLY);   
  183.             rs=pstm.executeQuery();   
  184.             rs.last(); // 移動到最後一行   
  185.             page.setAllRows(rs.getRow()); // 設置一共有多少行記錄   
  186.             // 算出有多少頁   
  187.             if (page.getAllRows() % page.getRowPerPage()== 0)    
  188.             {   
  189.                 page.setAllPages(page.getAllRows() / page.getRowPerPage());   
  190.             }   
  191.             else    
  192.             {   
  193.                 page.setAllPages(page.getAllRows() / page.getRowPerPage() + 1);   
  194.             }   
  195.             //判定是否是第一頁   
  196.             if (page.getCurPage() == 1//當前頁   
  197.             {   
  198.                 // 將指針移動到此ResultSet對象的開頭,正好位於第一行之前   
  199.                 rs.beforeFirst();   
  200.             }    
  201.             else    
  202.             {   
  203.                 // 將指針移動到此ResultSet對象的給定行編號   
  204.                 rs.absolute((page.getCurPage() - 1) * page.getRowPerPage());   
  205.             }   
  206.             int i = 0;   
  207.             while(rs.next() && i < page.getRowPerPage())   
  208.             {   
  209.                  Customer cu=new Customer();   
  210.                  cu.setId(rs.getInt("CUM_ID"));   
  211.                  cu.setName(rs.getString("CUM_FULLNAME"));   
  212.                  cu.setAddress(rs.getString("CUM_MAINADDRESS"));   
  213.                  cu.setPhone(rs.getString("CUM_PHONE"));   
  214.                  al.add(cu);   
  215.                  i++;   
  216.             }   
  217.         }   
  218.         catch(Exception ex)   
  219.         {   
  220.              ex.printStackTrace();      
  221.         }   
  222.         finally  
  223.         {   
  224.                try  
  225.                {   
  226.                    if(conn!=null)   
  227.                    {   
  228.                        this.conn.close();   
  229.                    }   
  230.                }   
  231.                catch(Exception ex)   
  232.                {   
  233.                    ex.printStackTrace();      
  234.                }              
  235.         }   
  236.         return al;   
  237.     }   
  238. }   
  239.   
  240. 4、頁面顯示   
  241.   <%@ page language="java" contentType="text/html;charset=GBK"%>   
  242. <%@ page import="java.util.*,bean.SepPage"%>   
  243. <%@page import="bean.Customer;"%>   
  244.   
  245. <%   
  246.     ArrayList all = (ArrayList) request.getAttribute("all");   
  247.     SepPage seppage = (SepPage) request.getAttribute("pagebean");   
  248. //獲取設置的SepPage參數   
  249. %>   
  250. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">   
  251. <html xmlns="http://www.w3.org/1999/xhtml">   
  252.     <head>   
  253.         <title>客戶管理</title>   
  254.         <link href="css/style.css" rel="stylesheet" />   
  255.     </head>   
  256.     <body>   
  257.         <div class="top" >   
  258.             『 作者管理 』   
  259.         </div>   
  260.         <div class="center">   
  261.             <form action="AuthorServlet?status=selectbylike&curPage=1" method="post" style="margin: 0;">   
  262.                 <table class="fine" cellpadding="0" cellspacing="0">   
  263.                     <tr>   
  264.                         <td colspan="3" style="height: 40px;">   
  265.   
  266.                         </td>   
  267.                     </tr>    
  268.                     <tr>   
  269.                        <td>   
  270.                               序號   
  271.                         </td>   
  272.                         <td>   
  273.                             客戶名稱   
  274.                         </td>   
  275.                         <td>   
  276.                             聯繫電話   
  277.                         </td>   
  278.                             <td>   
  279.                             地址   
  280.                         </td>   
  281.                         &nbsp;&nbsp;&nbsp;&nbsp;<td>   
  282.                             操作   
  283.                         </td>   
  284.                     </tr>   
  285.                     <%   
  286.                         int i=0;   
  287.                         if (all != null && all.size() != 0)   
  288.                          {   
  289.                             for (int j=0;j<all.size();j++)    
  290.                             {     
  291.                                 i++;   
  292.                                 Customer cus=(Customer)all.get(j);   
  293.                                 int authorId1=cus.getId();   
  294.                                 String str="找不到記錄!!!";   
  295.                                 if(i==0)   
  296.                                 {    
  297.                                        
  298.                                       
  299.                                    out.println("<scrtipt>alert('找不到記錄')<script>");   
  300.                                 }   
  301.                                 else  
  302.                                 {%>   
  303.                                       
  304.                         <tr>           
  305.                         <td>   
  306.   
  307.                             <%=i%>   
  308.                         </td>   
  309.                         <td>   
  310.   
  311.                             <%=cus.getName()%>   
  312.                         </td>   
  313.                         <td>   
  314.                             <%=cus.getPhone()%>   
  315.                         </td>   
  316.                             <td>   
  317.                             <%=cus.getAddress()%>   
  318.                         </td>   
  319.                         <td>   
  320.              <td><a href="CustomerServlet?status=selectbyid&id=<%=cus.getId()%>">更新</a>&nbsp;&nbsp;&nbsp;&nbsp;</td>&nbsp;&nbsp;   
  321.                            
  322.             <td><a href="CustomerServlet?status=delete&id=<%=cus.getId()%>" οnclick="if(confirm('是否刪除業務信息?')){return   true;}else{return   false;}">刪除</a>&nbsp;&nbsp;&nbsp;&nbsp;</td>   
  323.             <td><a href="addCus.jsp">添加</a></td>   
  324.                     </tr>   
  325.                                    
  326.                                <%}%>   
  327.                        
  328.                     <%   
  329.                         }//end for循環   
  330.                     }//end if all != null   
  331.                     %>   
  332.                     <%   
  333.                         if (seppage != null) {   
  334.                     %>   
  335.                     <tr>   
  336.                         <td colspan="3"  
  337.                             style="height: 32px; text-align: right; font-weight: bold; padding-right: 10px;">   
  338.                             <font color="#FF5BAD">一共有&nbsp;<font color="red"><%=seppage.getAllPages()%></font>&nbsp;頁&nbsp;&nbsp;當前在第&nbsp;<font   
  339.                                 color="red"><%=seppage.getCurPage()%></font>&nbsp;頁&nbsp;&nbsp;&nbsp;&nbsp;</font>   
  340.                             <%   
  341.                                 if (seppage.getCurPage() != 1//不是第一頁,則首頁,上一頁可用   
  342.                                 {   
  343.                             %>   
  344.                             <a href="CustomerServlet?status=selectbyall&curPage=1">首 頁</a>&nbsp;&nbsp;   
  345.                             <a   
  346.                                 href="CustomerServlet?status=selectbyall&curPage=<%=seppage.getCurPage()-1 %>">上一頁</a>   
  347.                             <%   
  348.                                 }   
  349.                             %>   
  350.                             &nbsp;&nbsp;   
  351.                             <%   
  352.                                 if (seppage.getCurPage() != seppage.getAllPages())//不是最後一頁,則有下一頁和末頁   
  353.                                  {   
  354.                             %>   
  355.                             <a   
  356.                                 href="CustomerServlet?status=selectbyall&curPage=<%=seppage.getCurPage()+1 %>">下一頁</a>&nbsp;&nbsp;   
  357.                             <a   
  358.                                 href="CustomerServlet?status=selectbyall&curPage=<%=seppage.getAllPages() %>">末   
  359.                                 頁</a>   
  360.                             <%   
  361.                                 }   
  362.                             %>   
  363.   
  364.                         </td>   
  365.                     </tr>   
  366.                     <%   
  367.                         }   
  368.                     %>   
  369.                     </table>   
  370.                     </form>   
  371. <form action="CustomerServlet?status=selectbylike&curPage=1" method="post" style="margin: 0;">   
  372.                 <table class="fine" cellpadding="0" cellspacing="0">   
  373.                     <tr>   
  374.                         <td colspan="3" style="height: 40px;">   
  375.                             <select style="height: 20px" name="sel">   
  376.                                 <option selected value="由客戶姓名">   
  377.                                     由客戶姓名   
  378.                                 </option>   
  379.                                 <option value="由地址">   
  380.                                     由地址   
  381.                                 </option>   
  382.                             </select>   
  383.                             &nbsp;&nbsp;   
  384.                             <input  type="text" name="in" style="width: 200px" /><input  type="submit" value="查 找" />      
  385.                         </td>   
  386.                     </tr>    
  387.                     </table>   
  388.                     </form>   
  389.     </div>   
  390.     </body>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章