aspose file tools*
The moose likes Servlets and the fly likes Web Programming/JavaBean question... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Web Programming/JavaBean question... " Watch "Web Programming/JavaBean question... " New topic
Author

Web Programming/JavaBean question...

Chris Stewart
Ranch Hand

Joined: Sep 14, 2002
Posts: 184
I'm planning out an application right now for my school. I'm going to be redoing our department website and making it dynamic to some degree. Say for example, I display the faculty page. The data for this page is going to be in our database, so it's easy for anyone to come back and change it. Ideally, I want to create an application scoped JavaBean that has a collection with this data in it. Then I can just display from there instead of hitting the database everytime. My question is this, how can I populate that JavaBean when the web server is started for the first time? If I didn't make myself clear, please let me know.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Unless your database access is exeptionally slow, or you have some other strong reason for avoiding the DB, I'd just get the data from the DB each time. By caching the data in a servlet context object, you won't be looking at real-time data and have to worry about invalidating/updating the cache when appropriate and so on.
That said, should you decide that caching the data makes sense, you can write a servlet that will read the data and load it into the cache in its init method, and placing a declaration in the web.xml that loads the servlet on server startup.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chris Stewart
Ranch Hand

Joined: Sep 14, 2002
Posts: 184
I was thinking of caching it because it really won't change that often (MAYBE once a month, if that). And when it does, we can always restart Tomcat.
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
If you're also developing the code for updating the faculty database table, it shouldn't be much a problem.
1) You can instantiate the javabean with an init servlet; in web.xml, define a servlet with a <loadonstartup></loadonstartup> numeric value - the server will load the servlets in order based on that value. In the servlet, put your initialization code in the init() method.
2) In the servlet init method, load the bean, then grab the ServletContext and set the bean as an attribute.
Viola! Bean initialization and application availability in one fell swoop. Don't forget to refresh the bean with new data whenever the table gets updated (assuming that's also part of your application).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Umm, isn't that what I said two posts ago?
when it does, we can always restart Tomcat.

Or... since this is a servlet, set it up so that a post to the servlet triggers a reload of the cache.
hth,
bear
Chris Stewart
Ranch Hand

Joined: Sep 29, 2001
Posts: 124
That's what I think I'm going to do. Triggering a reload seems a lot better than restarting the web server.
Thanks for the help guys.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Web Programming/JavaBean question...