On Fri, 26 Jul 2002, Gord Tomlin wrote:
> Date: Fri, 26 Jul 2002 13:03:53 -0400
> From: Gord Tomlin <[EMAIL PROTECTED]>
> Reply-To: Tomcat Users List <[EMAIL PROTECTED]>
> To: [EMAIL PROTECTED]
> Subject: Servlet init() called twice - another cause
>
> Yesterday, the init() method of a servlet of mine started to be called
> twice. The hash code of the servlet object proved that there were two
> separate instances of the servlet. The archives mention some causes of this
> situation, but mine was caused by another one. I'm posting this here in case
> it's of help to anyone else.
>
> >>From the archives:
> "From: Larry Isaacs
> Subject: RE: Servlet's init() method being called twice?
> Date: Fri, 9 Nov 2001 09:18:09 -0500
>
> This can occur if you have a web application under the auto-served
> "webapps" directory and also create a context for that same web
> application but with a different path, i.e.
>
> <Context path="/otherpath" docBase="webapps/mywebapp" ...
>
> You would get the same webapp served as "/mywebapp" and
> "/otherpath", hence two calls to your servlet's init(),
> one for each context.
>
> Larry"
>
> In my case, I first executed the servlet using the name contained in the
> <url-pattern> of a <servlet-mapping>. in web.xml; the second time the
> servlet was executed it was referenced by its full name relative to the
> webapps directory. When I changed the second reference to also use the name
> from the <url-pattern> the problem went away.
>
> It appears that the lesson here is that a given servlet should always be
> accessed using the same URL, or else Tomcat will treat each different URL as
> a reference to a "different" servlet.
>
It's actually a little bit more subtle than that.
The servlet spec guarantees you a single instance (assuming you are not
using SingleThreadModel) per servlet *definition*. So what's a
definition?
* In web.xml, each <servlet> element is a servlet definition.
It is entirely legal to have multiple <servlet-mapping> patterns
that point at the same <servlet> definition -- they will all
share the same instance. These definitions are set up when the
web app is first deployed (typically at Tomcat startup).
* At run time, Tomcat can create new servlet definitions dynamically
for you (this is not in the servlet spec, but is a very common
feature) using the "invoker" servlet that is mapped to "/servlet/*".
Whenever it sees a URL like "/servlet/{foo}", it assumes "{foo}" is
the class name of a servlet class -- the first time in, it creates
a new servlet definition and sets up a new servlet mapping with an
exact match, so that future requests are mapped directly to this
new definition.
As Gord noticed, a reference to a servlet definition through a particular
servlet mapping, and a reference to a servlet of the same class through
the invoker servlet, are considered to be two different definitions.
However, references to the same servlet definition through two different
servlet mappings would have referenced the same (single) servlet instance.
> Regards, Gord Tomlin
Craig
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>