主頁(yè) > 知識(shí)庫(kù) > Jsp真分頁(yè)實(shí)例---分頁(yè)

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

熱門(mén)標(biāo)簽:電銷機(jī)器人免培訓(xùn) 外呼系統(tǒng)使用方法 如何看懂地圖標(biāo)注點(diǎn) 自繪地圖標(biāo)注數(shù)據(jù) 海外圖書(shū)館地圖標(biāo)注點(diǎn) 電話機(jī)器人需要使用網(wǎng)絡(luò)嗎 潤(rùn)滑油銷售電銷機(jī)器人 南通通訊外呼系統(tǒng)產(chǎn)品介紹 給地圖標(biāo)注得傭金

網(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èn)網(wǎng)站等。所以,如果數(shù)據(jù)量相當(dāng)龐大,不建議使用用真分頁(yè)。

真分頁(yè):假分頁(yè)每次只取需要的數(shù)據(jù)返回給客戶端,比起真分頁(yè)沒(méi)有那么大的數(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)求客戶端,若沒(méi)有帶page參數(shù),說(shuō)明是第一次訪問(wèn),則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;iparems.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 ArrayListStudentBean> 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);
    ArrayListStudentBean> list = new ArrayListStudentBean>();
    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ù)是寫(xiě)入前臺(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è)一樣。。。

您可能感興趣的文章:
  • jsp分頁(yè)顯示的實(shí)現(xiàn)代碼
  • JSP分頁(yè)顯示的實(shí)例代碼
  • jsp實(shí)現(xiàn)上一頁(yè)下一頁(yè)翻頁(yè)功能(示例代碼)

標(biāo)簽:大連 廣州 樂(lè)山 南京 黃石 貸款邀約 銅川 內(nèi)江

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Jsp真分頁(yè)實(shí)例---分頁(yè)》,本文關(guān)鍵詞  Jsp,真,分頁(yè),實(shí)例,---分頁(yè),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Jsp真分頁(yè)實(shí)例---分頁(yè)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Jsp真分頁(yè)實(shí)例---分頁(yè)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章