You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Christopher Piggott <ch...@rochester.rr.com> on 2006/04/30 16:26:39 UTC

Classloading questions

As an experiment, I placed a file in the webapp dir of a servlet (not in
WEB-INF but in the directory above it) and attempted to read it using
getResource().  What I found was that I could not locate the resource unless
I used getServletContext().getResource().

Reading through the Classloading How-To, I am confused why this works.  The
how-to says that from the perspective of the webapp, loading takes place in
this order:

    * Bootstrap classes of your JVM
    * System class loader classses (described above)
    * /WEB-INF/classes of your web application
    * /WEB-INF/lib/*.jar of your web application
    * $CATALINA_HOME/common/classes
    * $CATALINA_HOME/common/endorsed/*.jar
    * $CATALINA_HOME/common/lib/*.jar
    * $CATALINA_BASE/shared/classes
    * $CATALINA_BASE/shared/lib/*.jar

But, what is the order for getServletContext() ?

To try to figure this out, I ran another experiment:

	ClassLoader c1 = this.getClass().getClassLoader();
	log(" this's classloader is " + c1.getClass().getName());

	ClassLoader c2 = getServletContext().getClass().getClassLoader();
	log(" servlet context's  classloader is " + c2.getClass().getName());

Results:

	this's classloader is org.apache.catalina.loader.WebappClassLoader
	servlet context's  classloader is
org.apache.catalina.loader.StandardClassLoader

The HowTo says the following classloaders exist:

    * Bootstrap
    * System
    * Common
    * Catalina
    * Shared
    * WebappX

How is that some instance of StandarClassLoader able to access the webapp
root?



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Classloading questions

Posted by Michael Echerer <me...@tngtech.com>.
Christopher Piggott wrote:
> As an experiment, I placed a file in the webapp dir of a servlet (not in
> WEB-INF but in the directory above it) and attempted to read it using
> getResource().  What I found was that I could not locate the resource unless
> I used getServletContext().getResource().
> 

Your basic assumption that getServletContext().getResource() works by
using classloader means is simply wrong.

If you look at the JavaDoc of ServletContext it says that getResource()
works file based (relative to the context root) and does not use the
classloader.

http://tomcat.apache.org/tomcat-5.0-doc/servletapi/javax/servlet/ServletContext.html#getResource(java.lang.String)


Cheers,
Michael


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org