aspose file tools*
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

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Hello. I am working on a project where i need to implement pagination features. Each page directs to the same page with next 2 records. The new thing is that every record in a page has 4 radio buttons. So, how can i store the user selected radio buttons after each jsp page? here is my code

Test1.jsp



Though its not a good practice to include scriplets, i did that.
There are 6 records in the database. This code displays 2records in a page. After pressing NEXT it displays next 2 records. How should i get the value of Radio button Selected?? Should i invoke a servlet after the Next button is pressed. That servlet should store those 2 answer & redirect to same page again? But i think that's not the right way to do it

Suggest something.
Thanks


kunal
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

Why not?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
After pressing NEXT it displays next 2 records. How should i get the value of Radio button Selected?? Should i invoke a servlet after the Next button is pressed. That servlet should store those 2 answer & redirect to same page again?


Is that the right way?
Ankit Kumar Yadav
Greenhorn

Joined: May 03, 2012
Posts: 3
Kunal, don't you think that you have messed up the code? I think that will be lot of mixing of codes
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

Ankit Kr wrote:Kunal, don't you think that you have messed up the code? I think that will be lot of mixing of codes

Huh? What on earth are you talking about?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

Kunal Lakhani wrote:Is that the right way?

What do you think?
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
I think there would be a better option. If i have to fetch 300records, 5 at a page, then that particular servlet will be invoked 60 times. Isn't that very bad style of coding?

Bear Bibeault, what do you suggest?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

Kunal Lakhani wrote:Isn't that very bad style of coding?


No. Why would it be?

Why fetch more records at once than needed?

What is the problem with performing a series of small fetches as they are needed by the user?

And only if they are needed?

Are you suffering from premature optimization syndrome?

Ankit Kumar Yadav
Greenhorn

Joined: May 03, 2012
Posts: 3
If i have to fetch 300records, 5 at a page, then that particular servlet will be invoked 60 times.



To me it seems a bad style of coding. Invoking the same servlet again & again for 60times doesn't sounds good
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Why fetch more records at once than needed?

What is the problem with performing a series of small fetches as they are needed by the user?

And only if they are needed?



Bear Bibeault, i am very much confused by your reply. I am fetching all the records at a time, storing into a list, and displaying as a series of small quantity.

Kunal Lakhani wrote:
Isn't that very bad style of coding?



No. Why would it be?

This means the jsp code is ok, and there would be no problem if i run the same servlet 60times. Is it so?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

Ankit Kumar Yadav wrote:To me it seems a bad style of coding. Invoking the same servlet again & again for 60times doesn't sounds good

Nonsense. Please explain why you think this? A servlet doesn't wear out -- why would it be "bad" to call it 60 times? 60,000 times? 60,000,00 times? Do you think it's going to get tired?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

Kunal Lakhani wrote:I am fetching all the records at a time, storing into a list, and displaying as a series of small quantity.

Why?

Why fetch more than you need at a time?

And if your answer is "efficiency" then I will accuse you of premature optimization.

Why make it more complicated than it needs to be unless and until you can prove that there's a problem with the simple approach.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
I thought that would be a better approach than to hit database for every 5 records
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

You keep saying "I thought this" without any reasons to back it up.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Since its a online test module, What i thought is that its better to get all records needed at a time, than to query 5 records at a time. That's the only reason.

Do you suggest to query 5 records at a time?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

You are bringing new information in.

Is it the case that all the records will always be viewed? That's a much different case than search results where it's unlikely that anyone will ever look past the 1st page.

What it comes down to is: do what's simplest first, and make it more complicated only if you have a really good reason to do so.

In the case of a 20-question test where there are 5 questions per "page", I might fetch all 20 at once and cache them in memory. Especially if they're the same for all users. If there are too many questions to hold in memory, then I'd likely fetch one page at a time.

Anything in between is much more complicated and I'd venture in that direction only if I had good reasons to.

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Sorry, for not giving all information in the beginning. Questions will be different for each users. Moreover, The questions will be randomly chosen each time. Test will be of 70Questions, 5 at a page. There will only be a NEXT button just to navigate to next page. That's it.

Will i need to cache them in the memory? Can i face any problem, if i store them in a list, and use the jsp page i mentioned earlier to display, and invoking servlet each time the page is navigated?



Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

OK, so given that, I'd choose the path of most simplicity.

If the questions were in order, I'd likely just fetch them a page at a time. Easy as pie!

But because the questions will be randomly chosen, we'd need to make sure that there aren't any repeats. That means that we'd need to keep track of which has been already used or not across views and that complicates things a bit.

So I might opt to fetch all 70, in random order, and hold them in the session for the user. Then we'd just need to know what page we're on, and do the math to fetch the right slice from the list of questions for each page view.

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks Bear Bibeault.
Then we'd just need to know what page we're on, and do the math to fetch the right slice from the list of questions for each page view.


Do i need to know what page we're on? Since the list doesn't contains any repeated Questions, (that's for sure), i don't think i need to work on slicing the questions.

Bear Bibeault, your suggestions has been great. Really, i mean it. Thanks a lot. :jumpingjoy:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

If you have 70 questions, already sorted in the order you will display them (take care of the randomizing up-front) you need to know the page number so you can select the 1st five, next five, then next five, and so on. You could also just keep track of the multiple of 5 that begins the sequence of five, but I thin it's easer to just count 1, 2, 3...

I always opt for the simplest way to do things. They get complicated enough on their own -- there's no need to over-complicate things needlessly.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks Bear Bibeault.

Looking forward to complete the module within 2days.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Scriplets let me down. I used it, and now i am stucked.

Now what i am doing is, sending 5 objects together to jsp , and jsp will then invoke the same method in servlet and then new 5Qs will be sent.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pagination Problem