Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classloading in tomcat

 
aBhijeet Iyengar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18156
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aaki Jain
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18156
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the getResourceAsStream, or if you have some special need to do it the hard way, use getResource and go from there.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic