Hi all:
Yes, a good discussion.
I puzzled over this for an application I wrote a while ago. After many
hours contemplating the servlet spec (2.2), I finally decided that if I
created an object and dropped it into the application context, it was
guaranteed to live as long as the servlet container. My logic was this:
- The servlet container can load/unload servlets at its pleasure
- A servlet marked "Load-on-startup" will be loaded and have its init()
method called when the container starts, even if its not mapped to a URL.
- Java won't finalize an instance so long as another instance or class has
a reference to it.
- The application context will maintain a reference to the instance.
- If the servlet container destroys the application context, it's probably
a sign that I should reinitialize everything anyway.
Could some of the folks who are more familiar with the specs and
implementation comment on whether this logic is correct and portable to all
containers? It worked for the application under Tomcat 3.x, but I've always
had nagging doubts.
Thanks,
Greg Trasuk, President
StratusCom Manufacturing Systems Inc. - We use information technology to
solve business problems on your plant floor.
http://stratuscom.ca
> -----Original Message-----
> From: Craig R. McClanahan [mailto:[EMAIL PROTECTED]]
> Sent: Monday, August 06, 2001 8:51 PM
> To: [EMAIL PROTECTED]
> Subject: Re: Such a thing as server startup class?
>
>
>
>
> On Mon, 6 Aug 2001, Erik Weber wrote:
>
> > > In a servlet 2.2 world, what most people do is declare a
> servlet to be
> > > <load-on-startup> and initialize things in the init()
> method of that
> > > servlet. This is relying on the container to never
> unload that servlet
> > > for the lifetime of the app (Tomcat doesn't unload
> anything until shutdown
> > > time), but it basically works.
> > >
> > > Craig McClanahan
> >
> > Good point. If the Servlet were to be unloaded, another
> instance would
> > need to be loaded right away. Is there any
> (platform-independent) way
> > to guarantee that at least one instance of a Servlet will always be
> > loaded, even if it's not set up to handle HTTP requests?
> >
>
> Sounds like time for my favorite URL reference - where to get
> the spec so
> you can look stuff like this up for yourself :-)
>
> http://java.sun.com/products/servlet/download.html
>
> On this particular issue, there is no such
> platform-independent API. In
> fact, the 2.2 and 2.3 specs explicitly remind you that you
> *cannot* rely
> on "servlet will stay loaded" behavior in a portable application. The
> container is free (as far as the spec is concerned) to unload a loaded
> servlet any time it feels like doing so.
>
> That was one of the primary reasons that application event
> listeners were
> added in 2.3 -- to give apps a portable mechanism that does
> not rely on
> non-guaranteed assumptions about servlet lifetime, or on container
> specific implementation features.
>
> > -Erik
> >
> >
>
> Craig
>
>