David Mossakowski wrote:

> No servlet engine I know of creates two instances of a servlet the way you
> describe it.  The init() method is "guaranteed" (from Servlet API) to be called
> only once at start up.
>
> You can store variables that will be common to all threads by putting them in
> context. (context.setAttribute())
>
> Check the servlet API.
>
> dave.
>

Apache JServ, for one, lets you run your servlets in more than one JVM for the
same servlet context.  In such a case, there really is an instance of the servlet
in each JVM (assuming there's been enough activity to cause them to be
initialized) -- and any instance variable in the servlet class is obviously not
shared in such an environment.  Within any one JVM, of course, there is only one
instance of a non-SingleThreadModel servlet that is accessed in a multithreaded
manner.

As David points out, though, ServletContext.setAttribute() is designed for sharing
global data for the same servlet, or for different servlets.  The servlet context
also serves as the "Application" scope in JSP, for the same reason.

If the difference between accessing a variable directly and via a Hashtable-type
structure makes a significant difference in your application's performance (the
original poster's justification for the SingleInstanceModel idea), you need to get
a better JVM or a little faster server.  Application design should *not* be driven
by such low-level optimization attempts, or you'll spend all your time down in the
details, and not on the big picture decisions that have much more dramatic
influence on the overall performance.

Craig McClanahan

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to