File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes how to use href links to call next search results page without instantiating objects again and again Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "how to use href links to call next search results page without instantiating objects again and again" Watch "how to use href links to call next search results page without instantiating objects again and again" New topic
Author

how to use href links to call next search results page without instantiating objects again and again

Dhamayanthi Karuppanan
Ranch Hand

Joined: Oct 20, 2009
Posts: 31
I have designed a search page with multiple input items to supply conditions for it, like name / number to filter records. The search results page would display 30 records per page and it will have next and back buttons (i have used href) to proceed or go back. I have completed all the coding to display records as i wanted.
I have coded as below:
1) As I show only 25 records per page, I have done a query to get just 25 records from db. When clicking on next button, I'm again querying database to get another 25 records. This will reduce runtime memory as I don't want to simply capture all the records and put it on session, because sometimes user may not view all 1000 records.
- Is this a better approach?

2)I have counted the no of records when the result pages shows, so that i can show "1 to 25 of 1000" records. But this count query runs every time i click on page next. How can I avoid calling this again and again, and putting the count on some scope variable

3) As my next and previous buttons are href links. Is it good to have them as it is, or it should be buttons? How can i call only "runSearchQuery()" when i click on next button. I dont want to instantiate the object again and again whenever I press next button.

Thanks for your time.
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
1) As I show only 25 records per page, I have done a query to get just 25 records from db. When clicking on next button, I'm again querying database to get another 25 records. This will reduce runtime memory as I don't want to simply capture all the records and put it on session, because sometimes user may not view all 1000 records.
- Is this a better approach?


You can have a mix approach where in you dont neccessarily hit database on every next click rather prefetch certain number of records (lets say 100 records in your case) and save them in session. Once you exhaust the records from session, you again prefetch the records in session and carry on from there.

I have counted the no of records when the result pages shows, so that i can show "1 to 25 of 1000" records. But this count query runs every time i click on page next. How can I avoid calling this again and again, and putting the count on some scope variable

If you are sure, this count is not going to change then you can set the count in session and not neccessarily hit the database on each next click.

As my next and previous buttons are href links. Is it good to have them as it is, or it should be buttons? How can i call only "runSearchQuery()" when i click on next button. I dont want to instantiate the object again and again whenever I press next button.

You can have href with image which gives user the impression as if they are clicking on button. As for runSearchQuery(), can you elaborate further on that as there is not enough information to comment on that.


Dhamayanthi Karuppanan
Ranch Hand

Joined: Oct 20, 2009
Posts: 31
Thank you very much for your answers Piyush Mangal.
1) I would do as you said.
2) I agree with storing count on session. but I'm not sure how to do with my design. Below is my code snippet:

Step 1:
searchpage.jsp -> has search elements and once form is submitted, Im calling another jsp to get search results. Below is my form.




Step 2:
on SearchResult.jsp



TrackerSearch is my java class where I use this class
1) to build search query from request parameter,
2) instantiated dao, search bean and result bean
3) pass query to dao to count --> tracker.runCountQuery();
4) and run search query -->tracker.runSearchQuery();
I hope, my jsp code will explain the flow.

Whenever I click on my next button, this searchresult.jsp will run and it performs all the steps i have given above. Actually I don't want to call (but, my design calls countquery) count on each and every time (i can put it on session) whenever I click on next button to get 25 records per page and im not intended to maintain the count if record is changed in meanwhile.
I want to call only tracker.runSearchQuery() whenever I press next. tracker.runSearchQuery() is a simple method that just calls db and queries records and puts on the arraylist which i get on .jsp page.

Please share your ideas.









Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
First of all, you should really refactor your code and move all the java code to a Servlet. Then in your servlet, set whatever data you want to show (i.e. TrackerSearchResultBean, startpos, endPostion etc.) in request attribute . You should store count in session scope so that you don't have to fetch it on each request. On JSP, use JSTL tags to show the data.
Dhamayanthi Karuppanan
Ranch Hand

Joined: Oct 20, 2009
Posts: 31
Thank you for your reply. But is there any way to do it from existing code? This is how other pages are designed and used. So I followed the same.
And as this is customization of existing code, I don't have that much flexibility to do. This web application is running on oracle server. If i need to use jstc, where should i place the jstl jar?
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
Well you can pretty much do the same thing in your jsp but that does not mean you should do it. I highly recommend that you let servlet do all the processing and then delegate view rendering to a JSP page. It should only be hard to change the code.

 
 
subject: how to use href links to call next search results page without instantiating objects again and again