Today, I looked at the page paging technology of JSP. By the way, I learned that the pages written this time are completely JSP pages, and there is no hierarchical processing. The pages are pure JSP pages, it is helpful to understand paging. I use MySQL for databases. Before paging, we need to define several variables, which are essential for paging. First, we need to know the size of each page for paging, so we have defined pagesize. When we retrieve data from the database, we need to define the variable startrow starting from the first few, the page number that we need to display, that is, pageno, also has a start data and end data value for each page, counterstart and counterend. The total number of records in the database is recordcount, and the total number of pages is maxpage, as well as the number of records prestart and nextpage on the previous and next pages.
<% @ Page contenttype = "text/html; charset = gb2312" %> <% @ page Language = "Java" %> <% @ page import = "Java. SQL. * "%> <% // driver name, which is old. If you use mysql5, change it by yourself. String drivername = "com. mySQL. JDBC. driver "; string username =" root "; // database username string userpasswd =" lifeifei "; // password string dbname =" pagetest "; // database name string tablename = "page"; // table name // connection string url = "JDBC: mysql: // localhost/" + dbname + "? User = "+ username +" & Password = "+ userpasswd; Class. forname (drivername ). newinstance (); connection = drivermanager. getconnection (URL); Statement statement = connection. createstatement (); int pagesize = 3; // number of records displayed per page int startrow = 0; // start to display the record number int pageno = 0; // number of pages to be displayed int counterstart = 0; // The initial value of each page number int counterend = 0; // the maximum value of the displayed page number int recordcount = 0; // The total number of records; int maxpage = 0; // the total number of pages int prevstart = 0; // The previous int nextpage = 0 ;/ /Next page // obtain the number of pages to be displayed. If (request. getparameter ("pageno") = NULL) {// if it is null, it indicates page 1st if (startrow = 0) {pageno = startrow + 1; // set to 1} else {pageno = integer. parseint (request. getparameter ("pageno"); // obtain the number of pages submitted by the user. startrow = (pageno-1) * pagesize; // obtain the record number displayed at the beginning} // because the number of displayed page numbers changes dynamically, it is impossible to display one hundred links at the same time if there are 100 pages in total. Instead, the initial value of the displayed page number is displayed based on the current page number. // a certain number of page links are displayed. // the initial value of the displayed page number is displayed !! If (pageno % pagesize = 0) {counterstart = pageno-(pagesize-1);} else {counterstart = pageno-(pageno % pagesize) + 1 ;} counterend = counterstart + (pagesize-1 ); %> <HTML>
The above is the complete paging code. It can be run directly.