1

I am populating a table in my jsp which has more than 20 records. Now, If I want to display each 10 records in a page and then the user clicks on next link to go to the other 20 records.

How can I do that in my jsp?

This is my current code

<table>
    <tr>
        <td>id</td>
        <td>name</td>
        <td>type</td>
    </tr>
<%
    while(resultSet.next())
    {
        %>
        <tr>
        <td><%=resultSet.getString(1)%></td>
        <td><%=resultSet.getString(2)%></td>
        <td><%=resultSet.getString(3)%></td>
    </tr>
<%
    }
%>
</table>
maas
  • 1,063
  • 3
  • 13
  • 13
  • 1
    Duplicate of [ResultSet to Pagination](http://stackoverflow.com/questions/1986998/resultset-to-pagination). Just a warning: you really don't want to do this in "raw" Java code in JSP. It'll on long term only give trouble in all colors. – BalusC Aug 02 '10 at 16:47

3 Answers3

1

I know that you are looking for a back-end solution, but how about making it easier for the user (by not forcing page refresh on each request) and have a js solution (a js pagination solution)?

A list of jQuery pagination solutions

Again, this is only a suggestion, go for whatever works for you (and your users) best.

As requested: see a good beginner tutorial or Another one.

adardesign
  • 33,973
  • 15
  • 62
  • 84
  • 1
    Hello my friend, I need a simpler tutorial, if you can find. I am a little bit beginner – maas Aug 02 '10 at 16:33
1

You can use 'display' tag which provides pagination, sorting, exporting. In your current code, you are iterating a result set and creating table rows. Having Java code in JSP is not the best practice. Move that into a servlet and create a collection of beans by iterating the result set and set the collection in request scope or session scope(If you want to avoid multiple DB hits).

Sample Usage (From display tag site)

<display:table name="sessionScope.test" pagesize="10">
    <display:column property="id" title="ID" />
    <display:column property="name" />
    <display:column property="email" />
    <display:column property="status" />
</display:table>

Here the 'test' is the name of the attribute that stores a collection. The 'property' attributes for column tag represent the properties of your bean contained in the collection. 'pagesize' specifies the pagination size that the number of items to be displayed in a page.

For more info: http://displaytag.sourceforge.net/1.2/index.html

Marimuthu Madasamy
  • 13,126
  • 4
  • 30
  • 52
0

What you want is a pagination code example. Here's one I found online: http://www.easywayserver.com/blog/jsp-pagination/ There are lots of examples of this and you'll have to find which one formats your nav links the way you like them

Dylan West
  • 627
  • 10
  • 22
  • Hello my friend, really it is helpful, but I am using oracle 10 g DB and there are some functions which are not supported like SELECT SQL_CALC_FOUND_ROWS Can you please help – maas Aug 02 '10 at 16:17
  • if you need to find the number of rows you can use a stmt like this: select count(*) from table_name where column_name = "something"; – Dylan West Aug 02 '10 at 16:43
  • What did you use for the table_name and for the column_name? And did you try this query directly in Oracle to see what it returns before trying it in the webpage? – Dylan West Aug 03 '10 at 17:41