Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Web Programming/JavaBean question...

 
Chris Stewart
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chris Stewart
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic