This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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
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).