欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Jsp真分頁(yè)實(shí)例---分頁(yè)

 更新時(shí)間:2017年04月11日 22:46:20   投稿:mdxy-dxy  
假分頁(yè)每次只取需要的數(shù)據(jù)返回給客戶端,比起真分頁(yè)沒有那么大的數(shù)據(jù)庫(kù)壓力。但也因?yàn)檫@個(gè)工作特性,所以假分頁(yè)的方法需要頻繁和服務(wù)器端進(jìn)行交互。既然頻繁交互,自然也會(huì)給服務(wù)器帶來(lái)負(fù)擔(dān)

網(wǎng)頁(yè)的分頁(yè)功能的實(shí)現(xiàn)比較簡(jiǎn)單,實(shí)現(xiàn)方法也多種多樣。

今天總結(jié)一個(gè)簡(jiǎn)單的Jsp真分頁(yè)實(shí)例。

首先,提到分頁(yè)就要先明確一個(gè)概念,何為真分頁(yè)何謂假分頁(yè)。

假分頁(yè):一次性從數(shù)據(jù)庫(kù)讀出表的所有數(shù)據(jù)一次性的返回給客戶端,由js來(lái)控制每一頁(yè)的顯示。

真分頁(yè):由程序控制,每一次只返回一頁(yè)大小的數(shù)據(jù),顯示到客戶端。

由此可以很清楚的分辨出真假分頁(yè)各自的優(yōu)缺點(diǎn):

假分頁(yè):由于一次性讀出所有數(shù)據(jù)并返回給客戶端,如果數(shù)據(jù)量龐大,所以這一次的動(dòng)作可能是非常消耗服務(wù)器資源和帶寬的,

但是返回給客戶端以后就非常輕松了,客戶在一段時(shí)間內(nèi)不會(huì)再像服務(wù)器端請(qǐng)求資源。但不代表可能出現(xiàn)一些意外情況,

比如說(shuō)客戶將瀏覽器關(guān)閉,重新訪問網(wǎng)站等。所以,如果數(shù)據(jù)量相當(dāng)龐大,不建議使用用真分頁(yè)。

真分頁(yè):假分頁(yè)每次只取需要的數(shù)據(jù)返回給客戶端,比起真分頁(yè)沒有那么大的數(shù)據(jù)庫(kù)壓力。但也因?yàn)檫@個(gè)工作特性,所以假分頁(yè)

的方法需要頻繁和服務(wù)器端進(jìn)行交互。既然頻繁交互,自然也會(huì)給服務(wù)器帶來(lái)負(fù)擔(dān)。

綜上:如果數(shù)據(jù)量較小,使用假分頁(yè)的效果會(huì)更優(yōu),如果數(shù)據(jù)量龐大,使用真分頁(yè)的效果更優(yōu)。

分析完特性,下面就來(lái)列舉一個(gè)簡(jiǎn)單的真分頁(yè)實(shí)例。

真分頁(yè)是通過(guò)程序來(lái)控制的,每次向數(shù)據(jù)庫(kù)請(qǐng)求需要的數(shù)據(jù)。

簡(jiǎn)述實(shí)現(xiàn)思路業(yè)務(wù)流程:

首先:客戶端帶著page參數(shù)請(qǐng)求客戶端,若沒有帶page參數(shù),說(shuō)明是第一次訪問,則page參數(shù)默認(rèn)為0;

其次:服務(wù)端根據(jù)page參數(shù),調(diào)用相關(guān)函數(shù),從數(shù)據(jù)庫(kù)中取出表中數(shù)據(jù),封裝成相關(guān)對(duì)象,返回給客戶端,并且返回新page參數(shù)及總頁(yè)數(shù);

最后:再客戶端顯示請(qǐng)求的相關(guān)數(shù)據(jù),并根據(jù)page參數(shù)及總頁(yè)數(shù)兩個(gè)參數(shù),決定上一頁(yè)下一頁(yè)的按鈕是否可用。

數(shù)據(jù)庫(kù)操作類:

public class DBBean {
  private Connection con;

  private PreparedStatement pstmt;
  private ResultSet rs;
  private String dbName ="test";
  private String dbuser = "root";
  private String dbpass ="******";
  
  static{
    try{
      Class.forName("com.mysql.jdbc.Driver");
    }catch(ClassNotFoundException e){
      System.out.println(e);
    }
    
  }
  
  public void prepareConnection(){
    try{
      con=DriverManager.getConnection("jdbc:mysql://localhost:3306/"+dbName,dbuser,dbpass);
    }catch(SQLException e){
      System.out.println(e);
    }
  }
  //關(guān)閉連接
  public void close(){
      try {
        if(con!=null)
          con.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      con = null;
      try {
        if(pstmt!=null)
          pstmt.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      pstmt = null;
  }
  //設(shè)置參數(shù)
  private void setParems(String[] parems){
    if(parems!=null){
      for(int i=0;i<parems.length;i++){
        try {
          pstmt.setString(i+1, parems[i]);
        } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  }
  public ResultSet executeQuery(String sql,String[] parems){
    ResultSet res = null;
    prepareConnection();
    try {
      pstmt = con.prepareStatement(sql);
      setParems(parems);
      res = pstmt.executeQuery();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally{
     }
    return res;
  }
}

學(xué)生類:

public class StudentBean {
  private long id;
  private String name;
  private String phone;
  private int age;
  private int score;
  public long getId() {
    return id;
  }
  public void setId(long id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getPhone() {
    return phone;
  }
  public void setPhone(String phone) {
    this.phone = phone;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public int getScore() {
    return score;
  }
  public void setScore(int score) {
    this.score = score;
  }
}

學(xué)生數(shù)據(jù)操作類

public class StudentDao implements StudentDaoIn {
@Override 
public ArrayList<StudentBean> findByPage(int page){
    DBBean db = new DBBean();
    int begin = (page-1) * 5;
    String sql = "select * from t_student limit "+begin+",5";
    ResultSet rs = db.executeQuery(sql,null);
    ArrayList<StudentBean> list = new ArrayList<StudentBean>();
    try {
      while(rs.next()){
        StudentBean st = new StudentBean();
        st.setName(rs.getString("name"));
        st.setAge(rs.getInt("age"));
        st.setId(rs.getInt("id"));
        st.setPhone(rs.getString("phnoe"));
        st.setScore(rs.getInt("score"));
        list.add(st);
      }
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return list;
  }
  @Override 
  public int userCount(){
    DBBean db = new DBBean();
    String sql = "select count(*) from t_student";
    ResultSet rs = db.executeQuery(sql, null);
    int count = 0;
    try {
      rs.next();
      count = rs.getInt(1);
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return count;
  }
}

相關(guān)業(yè)務(wù)邏輯

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String page = null;
    page = request.getParameter("page");
    if(page == null || page=="")
      page = "1";
    StudentDao studao = new StudentDao();
    request.setAttribute("student",studao.findByPage(Integer.parseInt(page)));
    request.setAttribute("pagenum",studao.userCount()/5+1);//總頁(yè)數(shù)
    request.setAttribute("page", page);//當(dāng)前頁(yè)
    request.getRequestDispatcher("student.jsp").forward(request, response);  
    
  }

前臺(tái)JSP代碼:

<table id="t_stu" border="1" cellpadding="2" cellspacing="0">
  <thead>
    <tr>
      <th>ID</th>
      <th>姓名</th>
      <th>年齡</th>
      <th>電話</th>
      <th>成績(jī)</th>
    </tr>
  </thead>
  <c:forEach items="${student}" var="st">
    <tr>
      <td>${st.getId()}</td>
      <td>${st.getName()}</td>
      <td>${st.getAge()}</td>
      <td>${st.getPhone()}</td>
      <td>${st.getScore()}</td>
    </tr>
  </c:forEach>
</table>
<br>
共 ${pagenum}頁(yè)  當(dāng)前 第${page}頁(yè) 
<c:choose>
  <c:when test="${page>1}">
    <a href="getSutent?page=${page-1}" rel="external nofollow" ><input type="button" value="上一頁(yè)" ></a>
  </c:when>
  <c:otherwise>
    <input type="button" value="上一頁(yè)" disabled="disabled" />
  </c:otherwise>
</c:choose>
<c:choose>
  <c:when test="${page!=pagenum}">
    <a href="getSutent?page=${page+1}" rel="external nofollow" ><input type="button" value="下一頁(yè)"></a>
  </c:when>
  <c:otherwise>
    <input type="button" value="下一頁(yè)" disabled="disabled" />
  </c:otherwise>
</c:choose>

本例是真分頁(yè)的一個(gè)簡(jiǎn)單實(shí)現(xiàn),有著明顯的缺點(diǎn)。

例如:

1.在后臺(tái)相關(guān)業(yè)務(wù)邏輯處,只對(duì)page做了簡(jiǎn)單的判斷,因?yàn)椴樵兿嚓P(guān)page時(shí),參數(shù)是寫入前臺(tái)a標(biāo)簽中的,所以懂技術(shù)的用戶,可以隨意改動(dòng)其值

由此查詢數(shù)據(jù)庫(kù)可能帶來(lái)意想不到的錯(cuò)誤。

2.功能不夠完善,僅提供了上一頁(yè)下一頁(yè)按鈕的簡(jiǎn)單功能。

另外:實(shí)現(xiàn)假分頁(yè)時(shí)可以結(jié)合ajax和json。以此可實(shí)現(xiàn)無(wú)刷新翻頁(yè),看起來(lái)功能和真分頁(yè)一樣。。。

相關(guān)文章

  • jsp防止跨域提交數(shù)據(jù)的具體實(shí)現(xiàn)

    jsp防止跨域提交數(shù)據(jù)的具體實(shí)現(xiàn)

    這篇文章主要介紹了jsp防止跨域提交數(shù)據(jù)的具體實(shí)現(xiàn),需要的朋友可以參考下
    2014-02-02
  • JSP出現(xiàn)中文亂碼問題解決方法詳解

    JSP出現(xiàn)中文亂碼問題解決方法詳解

    這篇文章主要介紹了JSP出現(xiàn)中文亂碼問題解決方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • hibernate中的增刪改查實(shí)現(xiàn)代碼

    hibernate中的增刪改查實(shí)現(xiàn)代碼

    在hibernate中的增刪改查的實(shí)現(xiàn)。hibernate是OR框架,也就是對(duì)象關(guān)系框架,有了 hibernate我們就不用再去寫SQL語(yǔ)言,我們只需要操縱對(duì)象去進(jìn)行增刪改查。這里今天寫的就是在如何應(yīng)用hibernate實(shí)現(xiàn)增刪改查。
    2009-01-01
  • 基于jsp+mysql實(shí)現(xiàn)在線水果銷售商城系統(tǒng)

    基于jsp+mysql實(shí)現(xiàn)在線水果銷售商城系統(tǒng)

    這篇文章主要介紹了全新基于jsp+mysql實(shí)現(xiàn)的一個(gè)在線水果銷售商城系統(tǒng),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • struts2的select標(biāo)簽用法實(shí)例分析

    struts2的select標(biāo)簽用法實(shí)例分析

    這篇文章主要介紹了struts2的select標(biāo)簽用法,較為詳細(xì)的分析了Struts2中select標(biāo)簽的功能、定義及使用技巧,需要的朋友可以參考下
    2015-09-09
  • jsp留言板源代碼三: 給jsp初學(xué)者.

    jsp留言板源代碼三: 給jsp初學(xué)者.

    jsp留言板源代碼三: 給jsp初學(xué)者....
    2006-10-10
  • JSP spring boot / cloud 使用filter防止XSS

    JSP spring boot / cloud 使用filter防止XSS

    這篇文章主要介紹了JSP spring boot / cloud 使用filter防止XSS的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • java servlet過(guò)濾器使用示例

    java servlet過(guò)濾器使用示例

    Servlet過(guò)濾器是通過(guò)一個(gè)配置文件來(lái)靈活聲明的模塊化的可重用組建。Servlet過(guò)濾器截請(qǐng)求和響應(yīng),以便查看、提取或操作客服端和服務(wù)器之間交換的數(shù)據(jù),下面我們用代碼看看他是如何使用的
    2013-11-11
  • java 截取字符串(判斷漢字)

    java 截取字符串(判斷漢字)

    jsp截取中文字符串 len為字節(jié)長(zhǎng)度
    2008-11-11
  • JSP中使用JSTL按不同條件輸出內(nèi)容的方法

    JSP中使用JSTL按不同條件輸出內(nèi)容的方法

    這篇文章主要介紹了JSP中使用JSTL按不同條件輸出內(nèi)容的方法,實(shí)例分析了JSP標(biāo)簽庫(kù)JSTL的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10

最新評(píng)論