In the context of the original question, you are correct. The servlet container
will only run init() once, and init() must complete successfully before either
request thread can be serviced. The method init() is inherently thread-safe and
that safety is provided by the servlet container.

However, there are some situations in which init() may be called more than once.
All of these situations occur when more than one servlet instance is created.

Many servlet containers support reloading when the servlet class changes on
disk. If you change the servlet class, and the servlet container reloads the
servlet, then init() will be called on the newly loaded class.

A servlet container is allowed to destroy a servlet instance. When a new request
comes in to the server, the servlet container will have to create a new servlet
instance and call init().

A servlet class that implements SingleThreadModel can cause init() to be called
multiple times. In this case, the servlet container is allowed to instantiate
multiple instances. The container will call init(0 for each instance.

Finally, a single servlet class can be instantiated more than once if multiple
servlet definitions using the same class code are in the servlets.properties
file. The servlet container instantiates an instance for each definition, and
calls init() for each instance.

"http://www.roseindia.net" wrote:
>
> init is graunteed to be called once once.
>
> ----- Original Message -----
> From: S. Chen <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Saturday, August 19, 2000 11:08 AM
> Subject: Is it possible init() be called twice?
>
> > Hi,
> >
> > Assume the servlet is loaded when it is first called. In that case,
> > if two requests called the servlet at  exactly the same time, is it
> > possible that the method init(...) be called twice? So that one needs
> > do some synchronization inside init(...) is needed?
> >
> > Or, init(..) is guaranteed to be called only once?

___________________________________________________________________________
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