Jsp real paging instance --- paging, jsp paging ---

Source: Internet
Author: User

Jsp real paging instance --- paging, jsp paging ---

The pagination function of web pages is easy to implement, and there are various implementation methods.

Today, we will summarize a simple Jsp real paging instance.

First, we need to clarify the concept of true paging and false paging.

False paging: All data in the table is read from the database and returned to the client at one time. js controls the display of each page.

Real paging: controlled by the program. Each time only one page of data is returned, it is displayed on the client.

The advantages and disadvantages of true and false pagination can be clearly identified:

False paging: because all data is read at a time and returned to the client, if the data volume is large, this operation may consume a lot of server resources and bandwidth,

However, it is very easy to return to the client, and the client will not request resources like the server for a period of time. But it does not mean that there may be some unexpected situations,

For example, the customer closes the browser and re-accesses the website. Therefore, if the data volume is large, we do not recommend using real paging.

Real paging: fake paging returns only the required data to the client each time, which is less pressure on the database than real paging. However, due to this feature, false paging occurs.

The method must frequently interact with the server. Since frequent interaction, it will naturally burden the server.

To sum up, if the data volume is small, the use of fake pages will be better. If the data volume is large, the use of real pages will be better.

After analyzing the features, we will list a simple real paging instance.

Real paging is controlled by a program and the data required for each request to the database.

Briefly describe Implementation ideas Business Process:

First, the client requests the client with the page parameter. If the page parameter is not included, the page parameter is set to 0 by default;

Second, the server calls related functions based on page parameters, extracts table data from the database, encapsulates the data into related objects, returns the data to the client, and returns the new page parameters and the total number of pages;

Finally, the client displays the request data and determines whether the buttons on the next page of the previous page are available based on the page parameter and the total number of pages.

Database Operations:

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: // localh Ost: 3306/"+ dbName, dbuser, dbpass);} catch (SQLException e) {System. out. println (e) ;}}// close the connection 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;} // set the 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.exe cuteQuery ();} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();} finally {} return res ;}}

Student:

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;  }}

Student Data Operations

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;  }}

Related business logic

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); // total page number request. setAttribute ("page", page); // the current page request. getRequestDispatcher ("student. jsp "). forward (request, response );}

Front-end JSP code:

<Table id = "t_stu" border = "1" cellpadding = "2" cellspacing = "0"> <thead> <tr> <th> ID </th> <th> name </th> <th> age </th> <th> phone number </th> <th> score </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> total page $ {pagenum} current page $ {page} <c: choose> <c: when test =" $ {Page> 1} "> <a href =" getsuent? Page =$ {page-1} "rel =" external nofollow "> <input type =" button "value =" Previous page "> </a> </c: when> <c: otherwise> <input type = "button" value = "Previous Page" disabled = "disabled"/> </c: otherwise> </c: choose> <c: when test = "$ {page! = Pagenum} "> <a href =" getsuent? Page =$ {page + 1} "rel =" external nofollow "> <input type =" button "value =" next page "> </a> </c: when> <c: otherwise> <input type = "button" value = "next page" disabled = "disabled"/> </c: otherwise> </c: choose>

This example is a simple implementation of real paging and has obvious disadvantages.

For example:

1. in the business logic of the background, only a simple judgment is made on the page, because when querying the related page, the parameters are written to the front-end a tag, so users who understand the technology, you can change the value at will.

Unexpected errors may occur when you query the database.

2. The functions are not complete. Only simple functions of the button on the previous page are provided.

In addition, ajax and json can be combined for false paging. In this way, you can achieve no additional page flip. It looks like a real page...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.