Meaningless Drivel is fun!*
The moose likes Tomcat and the fly likes Classloading in tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Classloading in tomcat" Watch "Classloading in tomcat" New topic
Author

Classloading in tomcat

aBhijeet Iyengar
Greenhorn

Joined: Jan 06, 2013
Posts: 1
Hi All,
I am not sure if this question is raised before or not here as I couldn’t find any link to them. Please guide me if I had missed some resource

I was going through the classloader document of Apache tomcat and was unable to relate 2 points which they have made .

The document which I had been following is :

http://tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html

Now at a point when they are describing about webappX classloader they say:

‘As mentioned above, the web application class loader diverges from the default Java delegation model (in accordance with the recommendations in the Servlet Specification, version 2.4, section 9.7.2 Web Application Classloader). When a request to load a class from the web application's WebappX class loader is processed, this class loader will look in the local repositories first, instead of delegating before looking. ‘

So as per my understanding when ever any class within a web application tried to load a resource present within the war file by the means of :
getClass().getClassloader.getResource()…
would ask the webappX classloader to load the respective resource , which in turn would look in its repository first (i.e. in the war file ) before asking its parent to look for it. Which I totally understand.

But just couple of lines down in the same document , they say

Therefore, from the perspective of a web application, class or resource loading looks in the following repositories, in this order:
• Bootstrap classes of your JVM
• System class loader classes (described above)
• /WEB-INF/classes of your web application
• /WEB-INF/lib/*.jar of your web application
• Common class loader classes (described above)

Now, I get confused here. How a class loading can have first step as bootstrap. I would be assuming from the perspective of web application class or resource loading should have first step of
/WEB-INF/classes of your web application
And then look into jars and then start going backward.

I am sure Apache document would be right.

Could you someone please clear the confusion I am having .

Thanks








Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16246
    
  21

I think what they are saying is that if you load a class in Tomcat, the WAR's classes in WEB-INF will be scanned before the TOMCAT_HOME/lib classes and therefore override the TOMCAT classes in case of conflict.

In any event, actually obtaining the classloader in a webapp isn't something I'd generally recommend. The request getResource functions are usually a lot simpler to work with.


Customer surveys are for companies who didn't pay proper attention to begin with.
Aaki Jain
Greenhorn

Joined: Feb 29, 2012
Posts: 3
Hi Tim,

Thanks a lot for the clarification

When you say pick it from request getResource , do you mean loading it through servletContext's getResourceAsStream?

Thanks
-Abhi
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16246
    
  21

You can use the getResourceAsStream, or if you have some special need to do it the hard way, use getResource and go from there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classloading in tomcat