*
The moose likes Servlets and the fly likes Global Bean for Servlets/JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Global Bean for Servlets/JSP" Watch "Global Bean for Servlets/JSP" New topic
Author

Global Bean for Servlets/JSP

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Where to post this question was a toss up between here and JSP so I opted for here. If it needs to be moved, well, you know the drill....
I have a resource file that I need loaded into a Bean that is made available for my entire web app. I was wondering what the best way to do this is and most efficient way.
Should I load it when the application server starts (Tomcat)?
Should I load it when the first request is made to the app?
Should I just read the resource file and not worry about a bean every time?
Thanks for any help. Depending on the response, I will probably have follow up questions on how to implemenet it.


GenRocket - Experts at Building Test Data
Eelco den Heijer
Ranch Hand

Joined: Jan 17, 2002
Posts: 61
We had a similar issue a while ago. We had an XML file that was used to initialize a component. Because it took about 5 second to read the entire file and initialize the component, we chose to initialize it at startup of the controller servlet (instead of deferring it to the first request). The web app was loaded at start-up, the servlet was loaded at startup, and in the init of the servlet we initialized the component. The component was implemented as a singleton.
Hope this helps.


== <br />Rgrds,<br />Eelco<br /> <br />SCJP, SCJD, SCBCD, SCWD, SCEA
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
I agree with the initialization method, but I say don't make it a singleton, but rather stick it into the servlet context, that way, it'll be available to the whole application, and you won't be crippled by the singleton pattern.
Joe Pluta
Ranch Hand

Joined: Jun 23, 2003
Posts: 1376
I've found that almost invariably things that I thought only needed to get loaded when the application started eventually required the ability to be reloaded without shutting down the application.
Thus, I tend to use a lazy initialization technique, where if the information doesn't exist, it gets loaded. I'll put all the related information into a single object and then store a static reference to that object. When I access the information, if the reference is null, I load the information. With this technique, reloading is as simple as clearing the object reference.
Joe
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Joe, sounds like a singleton approach. The only problem I have with your solution is I don't want to have to check for that object everytime I need it. I want to expect it to just be there. That is why I would prefer to load it when the app starts and just put it in the ServletContext. Is this a bad idea?
Joe Pluta
Ranch Hand

Joined: Jun 23, 2003
Posts: 1376
Originally posted by Gregg Bolinger:
Joe, sounds like a singleton approach. The only problem I have with your solution is I don't want to have to check for that object everytime I need it. I want to expect it to just be there. That is why I would prefer to load it when the app starts and just put it in the ServletContext. Is this a bad idea?

A man after my own heart. Does it cost too much to check for the value every time? Of course, you could put in a try/catch for null pointer exception; I wonder if that would have the same overhead? Anyway, at this point it's purely an implementation question. From a design standpoint, I think the various techniques are equivalent.
Joe
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Global Bean for Servlets/JSP
 
Similar Threads
Reloading a Resource Bundle
How dispatcher-servlet.xml works
Interesting tiles/struts making a menu question
Creating a custom scope... (kinda long)
Internationize a SelectItem list