Link to home
Start Free TrialLog in
Avatar of btretzanor
btretzanor

asked on

Paging a JSP page source code -required URGENT-

Paging a JSP page source code -required URGENT-
I need to paging some JSP pages in 20 item.. I DONT KNOW HOW!
i need a source code as example!
Avatar of bagi112599
bagi112599

//    On your JSP page:

       <%
         Vector vClients = (Vector)request.getAttribute("clients");
         int pageNo = Integer.parseInt((String)request.getAttribute("page"));
         int totalPages = Integer.parseInt((String)request.getAttribute("totalPages"));
         int totalCount = Integer.parseInt((String)request.getAttribute("totalCount"));
         boolean hasMorePage = (pageNo < totalPages);

      %>
<FORM name="FORM1" method=post action="/servlets/myServlet">
       <% //-- To hold paging logic -- %>
      <input type=hidden name="page" value="1">

      <center><FONT color=black face=Arial size=2>Search returned <b><%= totalCount %></b> results<% if(vClients.size() != 0) { %> (Page <b><%= pageNo %></b> of <b><%= totalPages %></b>)<%}%></FONT><center><br>

// display l20 customers from vClients:
     <% //--------------------- Table Data -----------------%>
        <%
          // Display message if there were no matches.
          if(vClients.isEmpty()){
            out.println("<TR bgColor=lightgoldenrodyellow>" +
                        "  <TD colspan=8><b><FONT face=Arial size=2>NO CLIENTS MATCHED YOUR SEARCH CRITERIA.</FONT></b></TD>");
          }

          int row=1;
          Iterator it = vClients.iterator();
          while(it.hasNext()){
            cInfo = (BasicClientInfoBean)it.next();
        %>


        <TR bgColor=<%  if ( (row%2) == 1 ) {%>lightgoldenrodyellow
                    <%} else {%>aliceblue <%}%> >
          <TD><jsp:getProperty name="cInfo" property="firstname" />&nbsp;<jsp:getProperty name="cInfo" property="lastname" />
               </TD></TR><%
            row++;
          } // end of while loop


         %><TR>


     //display nav bar at the bottom as:

                       <% if(pageNo>1) { %>
                  <b><FONT face=Arial size=2 color=white>
                  <a href="javascript:onclick=history.back()" onmouseout="window.status='';return true" onmouseover ="window.status='Previous Page';return true">
                  Previous Page</a></FONT></b>
                <%}%>
                <% if(hasMorePage) { %>
                  <% if(pageNo>1) {%>
                    <b>|</b>
                  <%}%>
                  <FONT face=Arial size=2 color=white><b>
                <a href="javascript:onclick=nextPage(<%= pageNo+1 %>)" onmouseout="window.status='';return true" onmouseover ="window.status='Next Page';return true">
                  Next Page</a></b></FONT>
                <%}%>

                &nbsp;
</FORM>
// where nextPage () is javascript on the page:

  function nextPage(page){
    document.FORM1.cmd.value="search";
    document.FORM1.page.value=page;
    document.FORM1.submit();
  }
// in your myServlet:
 int  NUM_PER_PAGE = 20;
 
 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      // A vector of BasicClientInfoBean objects.
      Vector vClients = new Vector();

     // Page numbering.
      int page = 1;
      try{
        page = Integer.parseInt(request.getParameter("page"));
      }catch(Exception e){
            // default to 1.
      }

      try{
           sql ="select ....";
           // TODO: get DB connection:
               con = getconnection...;
           Statement stmt = con.createStatement();
          ResultSet  rs = stmt.executeQuery(makeSQL());
           int count=0;
             while(rs.next() ){
                    count ++;
                    //add those client that are on page'th page:
                    if(((page - 1)* NUM_PER_PAGE + 1 <= count) && (count <= page * NUM_PER_PAGE)){
                         BasicClientInfoBean client = new BasicClientInfoBean();
                         client.setFirstName(rs.getString("FirstName"));
                         client.setLastName(rs.getString("LastName"));
                         vClients.add(client);
                    }
               }
      rs.close();
      stmt.close();
          vClients
      }catch(SQLException e){
          System.err.println("GroupServlet ->searchGroup()-> error:" + e.getMessage());
      }

       // Get the number of pages.
      int total = vClients.size();
      int totalPages = (int)Math.ceil((double)total/NUM_PER_PAGE);
      // Set request objects for use in the result screen.
      request.setAttribute("clients", vClients);
      request.setAttribute("page", "" + page);
      request.setAttribute("totalPages", Integer.toString(totalPages));
      request.setAttribute("totalCount", Integer.toString(total));
  }
above code is not pretended to be working, but
it is extracted from my working code and
intended to give you the basic idea.
Hope it helps.
ASKER CERTIFIED SOLUTION
Avatar of rajendra_rathod
rajendra_rathod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Rajendra,

I face this error when i view this page in the browser. It says

Error
-----

"[05/Jul/2001:13:50:36] warning (26668): JSP compilation error: java.lang.Excepti
on: JSP parse error (line 6) - USEBEAN tag is invalid, stack: java.lang.Exceptio
n: JSP parse error (line 6) - USEBEAN tag is invalid"                  

-----------------------------------------------------------
What's wrong with the bean? What is the iRows for? In your while statement you specify that the iRows must be > than the iRecordsNumber but you initiate the iRows = 0 at the beginning and it was never incremented.

Please help.

Thanks
Jason (clkong@apis.dhl.com)
U can email meif u wish to
jason,
did you try mine?
bagi,

Nope, I have not try yours. But I actually manage to modified rajendra's code without using the bean and it works.

Cheers
Jason
Force/accepted by

Netminder
Community Support Moderator
Experts Exchange

jason/btrezanor:

Having two accounts is a specific violation of the Membership agreement, and the fact that you abandoned a number of questions in one account and have a number of abandoned questions in the second makes the offense more egregious. You will be contacted by Admin.