JSP - Pagination


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: JSP - Pagination

  1. #1
    Join Date
    Feb 2006
    Posts
    1

    JSP - Pagination

    Hi Anybody can help. How to wirte logic for getting each time 10 records in browser and also how to implement the logic next, previouse, last, first buttons using in JSP. Please anybody can provide the code or template
    Thanks
    Kumar

  2. #2
    Join Date
    Feb 2006
    Posts
    3
    Here is a quick and dirty solution, sorry if the code is messy, this is my first project with jsp.
    Code:
    <%@page contentType="text/html"%>
    <%@page pageEncoding="UTF-8"%>
    <%@page import="java.sql.*"%>
    <%@page import="com.mysql.jdbc.*"%>
    
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Test</title>
        </head>
        <body>   
        <% 
        java.sql.Connection conn;
        
        String userName = "theo";
        String password = "secretpass"; 
        String url = "jdbc:mysql://localhost/theo"; // localhost = my server, theo is  
        // my database, change as appropriate
     
        // You can invoke the script as "index.jsp?action=filltable" to fill a test table
        
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        
        conn = DriverManager.getConnection(url,userName,password);
        java.sql.Statement st;
        String query;
        String action = request.getParameter("action"); //get parameter ?action=
        int pageAsInt = 0;
        
        if ((action != null) && action.equals("filltable")) {
            // Fill the test table
            for (int i = 0; i<= 30;i++) {
                query = "INSERT INTO theo VALUES('','Item " + i + "');"; // change table name 'theo' as appropriate for your situation
                st = conn.prepareStatement(query);
                st.executeUpdate(query);
            }
        } else {
            String p = request.getParameter("page"); // get paramerer ?page=
            if (p != null) {
                // Strip quotation marks
                
                StringBuffer buffer = new StringBuffer();
                for (int index = 0; index < p.length(); index++) {
                    char c = p.charAt(index);
                    if (c != '\' ') {
                        buffer.append(c);
                    }
                }
                p = buffer.toString();      
                pageAsInt = Integer.valueOf(p); converts string p to int pageAsInt
            } else {
                pageAsInt = 1; // no page provided, starting with 1
            }
            
            // because records start with index 0; page should be decremented (for instance, page=2 should start with offset 10 = page - 1 * 10)
            query = "SELECT * FROM theo ORDER BY id ASC LIMIT "+ (pageAsInt - 1) * 10 + ",10 ;"; // replace theo with your table name
            st = conn.prepareStatement(query);
            
            java.sql.ResultSet result = st.executeQuery(query);
            
            out.println("<table>");
            while (result.next()) {
                out.println("<tr>");
                out.println("<td>" + result.getInt("id") + "</td>");            
                out.println("<td>" + result.getString("name") + "</td>");
                out.println("</tr>");
            }
            
            out.println("</table>");
            st.close();
            
            // Calculate number of rows in the table
    
            query = "SELECT * FROM theo WHERE 1"; //replace theo with your table name
            st = conn.prepareStatement(query);
            st.executeQuery(query);
            result = st.getResultSet();
            int count = 0;
            
            while (result.next()) {
                count++;
            }
            st.close();
            
            int pages = (count / 10) + 1; // 20 results = 0-9, 10-19, 20 thus 3 pages
            
            out.println("<table> <tr>");
            // First page
            if (pageAsInt > 1) {
                out.println("<td> <a href=\"?page=1\"> << </a> </td>");
                
                // Previous page
                
                int previousPage = pageAsInt - 1;
                out.println("<td><a href=\"?page='"+ previousPage +"'\"> < </a></td>");
            } else {
                out.println("<td> Already at first page </td>");
            }
            out.println("<td><b> Page: "+ pageAsInt + "</b></td>");                
            
            if (pages > pageAsInt) {
                // Next page
                int nextPage = pageAsInt + 1;
                out.println("<td><a href=\"?page='"+ nextPage + "'\"> > </a></td>");
                // Last Page
                out.println("<td><a href=\"?page='"+ pages+"'\"> >> </a></td>");
           } else {
                out.println("<td>Already at last page</td>");
                
           }
            out.println("</tr></table>");
        }
        conn.close();
        
        %>
        </body>
    </html>
    Last edited by Theo_Smit; 02-26-2006 at 08:24 PM.

Similar Threads

  1. Servlets and JSP
    By Purnima in forum Java
    Replies: 1
    Last Post: 12-29-2005, 04:14 AM
  2. JSP to servlet and back again.
    By iceman42 in forum Java
    Replies: 1
    Last Post: 03-24-2005, 11:31 AM
  3. Replies: 2
    Last Post: 08-23-2001, 04:20 PM
  4. How to invoke C DLLs from a JSP
    By Mohan in forum Java
    Replies: 6
    Last Post: 03-02-2001, 02:42 PM
  5. JSP and the <SERVLET> tag
    By Dusty in forum Java
    Replies: 2
    Last Post: 04-07-2000, 01:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center