Results 1 to 5 of 5
  1. #1
    maas is offline Member
    Join Date
    Jul 2010
    Posts
    30
    Rep Power
    0

    Default JSP/Javascript Page Pagination

    Hello All,

    I am having a jsp which is listing more than 30records. I would like to have a page pagination to the table. I have been advised with javascript. So, how can I do that in an easiest way?

    Here is my code for the table:


    Java Code:
    <% 
    while(rs.next()){
    
    %>	<a href="cir_view.jsp?cir_id=<%=rs.getString("cir_id")%>" Title="View" onClick="return popup(this, 'Report')">
    <%=rs.getString("cir_id")%>
    </a>
    </span> </td>
    <td width="39%" ><span style="font-size: 8pt"><%=rs.getString("institution_name")%></span></td>
    <td  width="26%" align="left"><span style="font-size: 8pt"><%=rs.getString("requester")%></span></td>
    <td  width="10%" align="left"><span style="font-size: 8pt">
    <%=rs.getString("created_date")%></span></td>
    <td  width="3%" align="left"><a href="cir_delete.jsp?cir_id=<%=rs.getString("cir_id")%>" class="ask" onclick="target='_blank';">
    <IMG SRC="12.png" ALT="Delete" BORDER="0" ></a></td>
    <td  width="3%" align="left"><a href="cir_update.jsp?cir_id=<%=rs.getString("cir_id")%>" onClick="return popup(this, 'Report')"><IMG SRC="28.png" ALT="Edit" BORDER="0"></a></td>
    
    </div>
    </tr>
    <%
    
    }
    
    rs.close();
    
    %> 
    </td>
    Thanx
    Last edited by maas; 08-03-2010 at 08:40 AM.

  2. #2
    maas is offline Member
    Join Date
    Jul 2010
    Posts
    30
    Rep Power
    0

    Default

    I have tried this , but I did not success.

    It is showng only 2 records

    Can you pelase help


    XML Code:
    <form name ="form" action="Report1.jsp" method="post" >
    <%! int numPages = 0; %>
    <%
    String columnName = "";
    int count = 0;
    int totalCols = 0;
    int increment = 1;
    int numRows = 0;
    
    
    String startIndexString = request.getParameter("startIndex");
    
    if(startIndexString == null) {
    startIndexString = "1";
    }
    
    int startIndex = Integer.parseInt(startIndexString);
    
    try{
    
    totalCols = 3;
    
    
    %>
    	<table border="1" width="84%" align="center" bgcolor=#66CCFF bordercolor=#000000 height="137">
    				
    		<tr>
    		<div id="container">
    			<td colspan="7" height="65">
    			<p align="center"><b>Customer Information Request Form</b></td>
    		</tr>
    				<tr>
    			<td colspan="2" height="33">
    			<p align="center">
    			<span style="font-size: 8pt; font-weight:700">CIR_ID</span></td>
    			<td height="33">
    			<span style="font-size: 8pt; font-weight:700">Bank Name</span></td>
    			<td height="33" width="26%">
    			<span style="font-size: 8pt; font-weight: 700">Request From</span></td>
    			<td height="33" width="18%" colspan="3">
    			<span style="font-weight: 700; font-size: 8pt">Created</span><span style="font-size: 8pt; font-weight: 700"> 
    			Date</span></td>
    		</tr>
    <% 
    for(int j=0; j<=totalCols && rs.next(); j++) { 
    
    %>		
    <tr bgcolor="#FFFFFF" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#FFFFFF';">
    <td width="3%"><span style="font-size: 8pt"><%=j=j+1%></span></td>
    <td width="10%"><span style="font-size: 8pt">
    <a href="cir_view.jsp?cir_id=<%=rs.getString("cir_id")%>" Title="View" onClick="return popup(this, 'Report')">
    <%=rs.getString("cir_id")%>
    </a>
    </span> </td>
    <td width="39%" ><span style="font-size: 8pt"><%=rs.getString("institution_name")%></span></td>
    <td  width="26%" align="left"><span style="font-size: 8pt"><%=rs.getString("requester")%></span></td>
    <td  width="10%" align="left"><span style="font-size: 8pt">
    <%=rs.getString("created_date")%></span></td>
    <td  width="3%" align="left"><a href="cir_delete.jsp?cir_id=<%=rs.getString("cir_id")%>" class="ask" onclick="target='_blank';">
    <IMG SRC="12.png" ALT="Delete" BORDER="0" ></a></td>
    <td  width="3%" align="left"><a href="cir_update.jsp?cir_id=<%=rs.getString("cir_id")%>" onClick="return popup(this, 'Report')"><IMG SRC="28.png" ALT="Edit" BORDER="0"></a></td>
    </div>
    </tr>
    
    
    <%
    }
    List list = new ArrayList();
    
    for( int i=0 ; i<100 ; i++){
    
    list.add("item"+i);
    
    }
    
    numRows = list.size();
    
    out.println(" total no. of records : "+ numRows );
    
    int numRecordsPerPage = 5;
    
    out.println(" Num of Records per page : " + numRecordsPerPage + "\n" );
    
    numPages = numRows /numRecordsPerPage ;
    
    int remain = numRows % numRecordsPerPage ;
    
    if(remain != 0){
    
    numPages = numPages +1 ;
    
    }
    
    out.println(" \n no. of pages : " + numPages );
    
    if((startIndex + numRecordsPerPage) <= numRows) {
    
    increment = startIndex + numRecordsPerPage ;
    }
    else{
    
    if (remain == 0){
    
    increment = startIndex + numRecordsPerPage ;
    
    }else{
    
    increment = startIndex + remain;
    }
    }
    
    for(count = startIndex; count < increment; count++) {
    
    %><tr><%
    for(int i=1; i<=totalCols; i++) {
    
    %><td><% out.println(list.get(count-1)); %></td><%
    }
    %></tr><%
    
    }
    %>
     
    </td>
    </table>
    Displaying Records:
    <% if(startIndex + numRecordsPerPage < numRows){%>
    <%= " " + startIndex %> - <%= increment - 1 %>
    <%}else{%>
    <%= " " + startIndex %> - <%= numRows %>
    <%}%>
    
    <%if(startIndex != 1) {%>
    <a href="Report1.jsp?startIndex=<%=startIndex-numRecordsPerPage%>">Previous</a>
    <%}%>
    
    <%increment += numRecordsPerPage ;%>
    <%if(startIndex + numRecordsPerPage <= numRows){%>
    <a href="Report1.jsp?startIndex=<%=startIndex+numRecordsPerPage %>">Next</a>
    <%}%>
    </tr>
    </table>
    <%
    }catch(Exception exc){
    out.println(exc.toString());
    } // end try-catch
    %>
    
    	<p>&nbsp;</p>
    </form>
    
    
    </body>
    <%
    conn.close();
    rs.close();
    %>

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Can I recommend splitting this up into a JSP and a servlet?

    The servlet gets the data from the db for the correct page (using suitable SQL syntax for the query), and then turns the result set into a List of objects of a class representing that data. It is this List that the JSP will loop over and display.

    The "next" and "previous" page buttons will simply fire a call to the servlet with the correct page number (currentPage + 1 for next, currentPage - 1 for previous).

    That's the proper structure for this sort of thing.

  4. #4
    maas is offline Member
    Join Date
    Jul 2010
    Posts
    30
    Rep Power
    0

    Default

    I am using it in a portal and there is some restrictions.

    If I want to modify my jsp, how can I do that?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Java mixed into JSPs like that is very difficult to read, and difficult to debug.

    For starters, right at the bottom of the code you post you have the connection and resultset close() calls back to front...and appear to have completely missed the close() for the statement. And they're not in a finally block.

    This is something that's a lot harder to miss in a properly structured app (though people still succeed in doing that).

    Whether this is a portal or not is no excuse for abusing a JSP. Is this some sort of project or company policy to write code like this then?

Similar Threads

  1. Replies: 2
    Last Post: 03-23-2009, 09:02 AM
  2. pagination
    By akhi in forum New To Java
    Replies: 1
    Last Post: 12-31-2008, 10:51 AM
  3. represent Double as "" instead of 0.0 in .jsp page without javascript
    By Tokajac in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-07-2008, 02:49 PM
  4. scriplet in javascript function in jsp page
    By Renjini in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-21-2008, 01:07 PM
  5. javascript sliderbar problem with JSP page
    By prakash.eng in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 12-17-2007, 08:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •