Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Displaying 10 records a time

  Asked By: Boell    Date: Jan 05    Category: Java    Views: 1293
  

Can anyone help me in developing a jsp that displays 10 records at a time from
120 records?
I do not want to hit the database everytime. Can it be done using arraylists?
Is anyone familiar with using CachedRowSet?

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Jamie Williams     Answered On: Jan 05

This is a very simple way of doing it. Note, I did not clear the
cache. So the loaded table will permanently be loaded until user
session dies. Also if 1000 users load the page at the same time,
there'll be 1000 copies cached.

Let say your jsp  page is named "listing.jsp"

listing.jsp

Request parameter
name: page
value: any number
description: Display page number x of the 120 records. Eg. 0 will
list records  1 to 10, 2=>11 to 20, and so on.

In listing.jsp

<%
synchronized (session) {
// if session attribute ArrayList "tablecache" is null
// then
// query db and add records to session attribute "recordcache"
// endif

// Note: consider storing in application scope to save memory
// Note: but need to release object once in a while.
}

ArrayList records = (ArrayList)session.getAttribute("tablecache");

int showPage = 0; // default
try {
// parse parameter "page"
// Set showPage to parsed value
} catch (NumberFormatException e){
// ignore
}

int lower = showPage * 10;
int upper = lower + 10;

// Check boundaries

// Display your records
for (int i=lower; i<upper; i++){
out.println(records.get(i));
}
// create navigation bar here
%>

 
Didn't find what you were looking for? Find more on Displaying 10 records a time Or get search suggestion and latest updates.




Tagged: