You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Rob Shaw <sh...@servidium.com> on 2000/11/16 11:53:20 UTC

Classloader

Given that I have 2 "contexts" defined (which I'm assuming
each has thier own classloader).

After making a request to the same servlet within each
context, I'm witnessing the creation of seperate instances
of the servlet per context, which I would expect, but
I'm also witnessing the sharing of classes
that have static members.

Is this intentional, or is this a bug?

According to Servlet 2.2 spec:

> 4.6 Reloading Considerations
>...
> Therefore, when a Container Provider implements a class
> reloading scheme for ease of development, they must ensure
> that all servlets, and classes that they may use, are loaded
> in the scope of a single class loader guaranteeing that the
> application will behave as expected by the Developer.
...

Thanks,
Rob.



Re: Classloader

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Rob Shaw wrote:

> Given that I have 2 "contexts" defined (which I'm assuming
> each has thier own classloader).
>

That is correct.

>
> After making a request to the same servlet within each
> context, I'm witnessing the creation of seperate instances
> of the servlet per context, which I would expect, but
> I'm also witnessing the sharing of classes
> that have static members.
>
> Is this intentional, or is this a bug?
>

It depends on where the classes are loaded from.  The following details
are Tomcat-specific, but something similar is probably true for most
containers.

The web-app class loader that Tomcat creates for each web app will
delegate to the system class loader (i.e. the one that processes the
CLASSPATH) if a class is not found in "/WEB-INF/classes" or
"/WEB-INF/lib/*.jar".  In such a case, there will be only one copy of
such classes (because the system class loader is delegated to from each
web app), so statics are shared across web apps.

On the other hand, if you made a copy of the class underneath WEB-INF
for each web app, the statics would *not* be shared.

>
> According to Servlet 2.2 spec:
>
> > 4.6 Reloading Considerations
> >...
> > Therefore, when a Container Provider implements a class
> > reloading scheme for ease of development, they must ensure
> > that all servlets, and classes that they may use, are loaded
> > in the scope of a single class loader guaranteeing that the
> > application will behave as expected by the Developer.
> ...
>

In fact, there is an explicit documented restriction in Tomcat that it
only applies to classes found under WEB-INF -- class reloading for
things found on the system class path is not supported.

>
> Thanks,
> Rob.

Craig