Quoting [EMAIL PROTECTED]:

> hi,
> 
> I have a question regarding data objects that should be accessible from 
> the entire application and which must be created at container startup time.
> 
> I need to either create and load an existing index or load a serialized 
> index if one exists. This can be a time-consuming process, and I would 
> like to do it once when the servlet container starts, then keep the 
> index in memory thereafter and available to the entire web application.
> 
> I've seen a few different ways of doing this by searching the archives, 
> but there wasn't a definitive best practice solution that I came across, 
> and the struts API has also changed since many of the suggestions were 
> offered.
> 
> At present, I am extending the ActionServlet and taking care of the 
> initialization stuff in the subclass's init(). I place the generated 
> index in the ServletContext. This ensures that the index creation will 
> occur only once, at container startup, and that it will be available to 
> the action classes of that web application, but I'm not sure if this is 
> the best solution. Also, I have just come across plugins for the first 
> time, and it seems that they also provide a way of doing 
> application-specific initialization. Having the index creation code in 
> the ActionServlet, as it is at present, seems messy to me, so perhaps 
> that is not the best place for it.
> 
> In short, what is the best method for ensuring that a global index is 
> created exactly once, and at container startup time? At present, I am 
> subclassing ActionServlet, and doing it there, but is the plugin 
> mechanism an alternate (better?) solution? Is the ServletContext the 
> appropriate place for global data?
> 
> thanks in advance,
> 
> n.
> 

If you are in a Servlet 2.4 environment, use a ServletContextListener -- this is
exactly what they are defined to do.  Your contextInitialized() method is
called once, before any requests are accepted by the app, and
contextDestroyed() is called exactly once when the app is removed (or the
server is shut down).

If you are in a Servlet 2.3 environment, use a Struts PlugIn.  Under most
scenarios, this will have the same effect (as long as you set the
load-on-startup attribute in web.xml); however servlet containers are allowed
to unload and reload a servlet during the lifetime of an application, which
would cause the initialization to be redone at reload.  Fortunately, containers
don't tend to do that -- especially not to a servlet that will get invoked as
often as the Struts controller servlet does.

Craig McClanahan


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to