aspose file tools*
The moose likes JSP and the fly likes preload select boxes with content Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "preload select boxes with content" Watch "preload select boxes with content" New topic
Author

preload select boxes with content

Dave Bosky
Ranch Hand

Joined: Dec 16, 2003
Posts: 72
I have a JSP page that contains 3 select boxes that I need populated with dynamic content. What's the best method for preloading form select boxes with name/values? The information doesn't change very often so I was wondering if I could load each collection into a application/global scope and loop over the collection as needed in my jsp's without hitting the database/server.

~Thanks,
Dave
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61043
    
  66

Absolutely. Why hit the DB each time if the data will not change? If your app needs to be sensitive to the occasional DB change, the cached data needs to be able to detect that and reload accordingly, but that's a frequently used and understood pattern if you need to employ it.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Thejaswi Narayana
Greenhorn

Joined: Apr 25, 2004
Posts: 27
The application I am working on requiries loading of selection box with data from the database. We are currently loading the data by making a database call to the database for each request. This is making the page loading very slow.Your suggestion of getting the data once from database and storing in the application scope will make the application fast. But if there is any change in the database data how to detect the change and reload the data. Please provide the link to the pattern which you mentioned.
Rajiv Babu
Greenhorn

Joined: Nov 23, 2004
Posts: 12
Hi,

Can you give us some more details as to how you are caching? How is the cache served across concurrent access. Assuming that you are using a servlet which will take care of all requests from the JSP, you can cache the data at the application level, by putting in the information as <attribute,value> in the servlet session context. You can retrieve any data by accessing the attribute in the sessionctx. Once an change happens in the data, the code should automatically modify the corresponding attribute in the servlet. You can remove it using the removeAttribute() call in javax.servlet.ServletContext. You can recreate the value using putAttribute(). That way the data in the cache is refreshed. Your JSP can then be rendered with the new data.

regards,
Rajiv
Thejaswi Narayana
Greenhorn

Joined: Apr 25, 2004
Posts: 27
What I wanted to know is how do I detect a change in the data in the database table. Whether the data is stored in the appication scope or session scope, it has to be updated as and when the data is updated or new row is added to the database table. Is it done by periodically doing a query on the database and updating the values stored or is there any standard pattern which is used for this.

Regards
Thejaswi Narayana
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

One easy way of doing it would be to have a single object that is responsible for updating the database for that table, and for caching the retrieved values. This object will refresh the cache as well, whenever it is asked to do an update.
All other objects must do the database access for that table through this 'manager' object.
You must make sure that there is only one instance of this 'manager' object in the application.
Thejaswi Narayana
Greenhorn

Joined: Apr 25, 2004
Posts: 27
The approach of using a manager object such as a DAO works fine if the database update is through the same application. But in my application, the database updates are through a different application and my application just allows to do a query on those tables using some search criteria.
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Yup, and provided no body updates the database directly by using a SQL client.

I wonder if any databases support triggers that can call a remote method or a web service whenever data is modified.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: preload select boxes with content