Rob Schoening wrote:

> Does anyone know if the Servlet spec makes any guarantees about a servlet's
> lifecycle?  That is, are servlet engines free to load and unload servlets at
> will?
>

This is a pretty accurate summary of the rules.  You cannot count on servlets
staying loaded.

>
>  I started to wonder if maintaining state information in the servlet object
> itself might be a violation of the spec.
>

Maintaining state information about a particular user's interactions in instance
variables of the servlet isn't necessarily a violation of the spec, but it's not
going to work correctly in most environments.  Many servlet engines call a
single instance of your servlet from multiple threads simultaneously, and the
instance variables can get overwritten.  However, there is no problem storing
shared state information in these instance variables.

Now, there's another complication to consider.  You also have no guarantee that
there is only one instance of your servlet in existence (for example, Apache
JServ lets you load balance across multiple back-end servers, possibly running
on different machines), so you can't even assume that a servlet instance
variable (storing shared state) is "global" to your servlet.  Think about
implementing a simple hit counter ... the simple approach would be to store the
current count in a servlet instance variable, and update it as needed.  But if
there are multiple instances of your servlet, there are also multiple instances
of your counter!

The moral of the story ... you need to understand the environment that your
servlet is going to execute in, design accordingly, and document any assumptions
you've made for the benefit of the person deploying your application.

>
> Rob
>

Craig McClanahan

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JSP-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to