wood burning stoves 2.0*
The moose likes JSP and the fly likes pagination problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "pagination problem" Watch "pagination problem" New topic
Author

pagination problem

Maria Smith
Ranch Hand

Joined: Apr 01, 2004
Posts: 40
Hi,

Can some one please help me with pagination problem?

I know how to implement pagination in my jsp pages using arraylist and loop through the resultset.

for (int i = offset.intValue(),l = Math.min(i + maxPageItems, list.size());i < l; i++)

But for one of my application I am getting data from database and storing it into a hash Map using linked list.

I want to implement pagination in jsp page . But I don't know how to do pagination for Collection and is to iterate through each element . Can some one please help me with this.

This is how my code looks right now

List list= null;
HashMap hMap = null;

if(session.getAttribute("RESULT") != null)
{
hMap =(HashMap) session.getAttribute("RESULT");
list = new ArrayList(hMap.keySet());


if(list.size() != 0)
{

for(Iterator iterator = list.iterator();iterator.hasNext();)
{
if(iterator.hasNext())
{
String name = (String)iterator.next();
LinkedList result1 = (LinkedList)hMap.get(name);

DataBean db = (DataBean)result1.get(0);

for (int i=0; i<result1.size() ; i++)
{
db = (DataBean)result1.get(i);


}
}
}
}
}


Gurus please help me.

Thanks,
Maria
Maria Smith
Ranch Hand

Joined: Apr 01, 2004
Posts: 40
Please some one help me
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60063
    
  65

I am a firm believer that paginination is something that should be handled at the database level. Why pass a bunch of data to the presentation layer that you aren't even going to show?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
You can use some ORM tool like hibernate for pagination in the data access

object and send only some predefined number of rows from dao like first 20

rows like that.


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Maria Smith
Ranch Hand

Joined: Apr 01, 2004
Posts: 40
Thank you for your reply.

But I need all the data in presentaion layer. Only reason I am using pagination is because I can't display all for Ex: 10000 rows on single page and I don't want to go to servlet every time to get next set of data.

Naseem,
I am not familiar with this ORM tool for pagination in the data access. Can you please give me some example on how to do that.

Thanks,
Maria
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60063
    
  65

Originally posted by Maria Smith:

But I need all the data in presentaion layer. Only reason I am using pagination is because I can't display all for Ex: 10000 rows on single page and I don't want to go to servlet every time to get next set of data.


Why? That's what databases are for. You still haven't explained why you want to do this in such an odd fashion.
[ July 11, 2006: Message edited by: Bear Bibeault ]
Maria Smith
Ranch Hand

Joined: Apr 01, 2004
Posts: 40
This is what I used before. I don't no any other way of achieving the same result. Please let me know if there is another way of doing this.

Thanks,
Maria
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60063
    
  65

Only ask the database for the slice of data that you need to display on a single page.

The SQL syntax to do this is different for each DB unfortunately. So you'll need to look it up for your DB.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Bear Bibeault:

The SQL syntax to do this is different for each DB unfortunately. So you'll need to look it up for your DB.


Some of the common ones are documented in our JSP FAQ:
http://faq.javaranch.com/view?PaginationOrPaging


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Hi Maria,
You can go for JDBC api for that as explained in the url posted by Ben.

Well if you want to do same thing using Hibernate. Then you can se Query or Criterion interface.

You need to create a query using session.



Its a very simple code.

You can find more details of it at Pagination and Pagination code


Naseem
[ July 11, 2006: Message edited by: Naseem Khan ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60063
    
  65

Switching to Hibernate just to implement simple paging is a mighty big step. And as with all framewroks, one should understand the basics before emplying one.
Yuvan Shankar
Greenhorn

Joined: Jul 11, 2006
Posts: 1
Naseem,
what if we dont have access to DB. Say there is a customer Service which will return the list of customers. And take our requierment like to list all these customer to user using pagination.
So in this case obviously we need to use some collection object and loop through it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60063
    
  65

That's an entirely different matter and you should start your own topic if you'd like to pursue it.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
A useful method if the List is entirely in memory is the List.subList() method.



You can then just iterate through the displayList, knowing it will produce the right items.
You just have to keep track of the from/to parameters.

There is a tradeoff between database usage, and memory usage here. To me, the solution depends upon the context. If there are 10million rows in the database, I'm not going to load them all into memory, just to display the first 20.
Also if there are hundreds of users, you don't have the memory space to give them their own session variables in memory.

However at the other extreme, if it is an expensive query, returning relatively few records, there can be a case for keeping things in memory (session variables).

Just my 2 cents,
evnafets
Maria Smith
Ranch Hand

Joined: Apr 01, 2004
Posts: 40
Thanks everyone for the response.

Maximum rows I am retrieving from the database is 5000 and most of the time it will be less then that
based on selection criteria. So i think i can afford that to keep in memory and display it.
But I am not sure how to create URL link for the list the way it looks in pagination.
Please let me know if you have any ideas.

Thanks,
Maria
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Well one idea which comes in my mind is by query string.

In jsp you have no. of links like...

page 1 2 3 4 5 6 and so on



Suppose if a user clicks link 1, then you want to show 1 to 10 rows.

In servlet, retrieve the value of query string which comes as a part of request.




Naseem
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pagination problem
 
Similar Threads
pagination
To do Pagination from arraylist in jsp
Pagination
Please help with pagination in JSP?
help required with pagination