Tomcat 5.5.23 documentation read tips 9-class loading

Source: Internet
Author: User
This article describes the logic of Tomcat classloading.

1. The key to classloading is to solve the problem of missing classes and missing resource files during development. To solve this problem, you must understand where the classloader of Tomcat locates for class and resources.

2. The first is a summary, telling us how to prevent our class and resources:

(1) for classes and resources specific to a particle web application, place unpacked classes and resources under/WEB-INF/classes of your web application archive, or place jar files containing those classes and resources under/WEB-INF/lib of your web application archive.
(2) for classes and resources that must be shared within SS all web applications, place unpacked classes and resources under $ catalina_base/shared/classes, or place jar files containing those classes and resources under $ catalina_base/shared/lib.

The general meaning is, if the resource and class file is only for a web app, then please put the class file, the resource file to the/WEB-INF/classes directory, if you have typed the class or resource file into a jar package, or some third-party jar packages, put it in the/WEB-INF/lib directory; if these classes and resource files are to be shared with other Web apps, put them in the $ catalina_base/shared/classes, $ catalina_base/shared/lib directory. Here, $ catalina_base is generally $ catalina_home, but $ catalina_base is used when multiple Tomcat servers are started, because the $ catalina_home of multiple Tomcat servers is not necessarily the same.

3. then the article details some Tomcat classloader and Their logic, and explains the summary above. We can see that the final classesloader, webappx, is responsible for loading resources in a webapp, however, a classloader called shared loads the resources in the $ catalina_base/shared directory. This is why we can find and load things in these places. For details, refer to this document, which describes in detail, including the classloader load under the common directory under Tomcat (that is, a classloader named common. For example, the classloader named Catalina is responsible for loading Tomcat's own classes, so this classloader is invisible to webapps. Here is the relationship diagram of classloader in Tomcat:

 

Bootstrap
|
System
|
Common
/\
Catalina shared
/\
Webapp1 webapp2...

 

4. The final document provides a very useful thing, that is, for a web app, Tomcat searches for classes and resources in the following order:


    Bootstrap classes of your JVM
    System Class Loader Classses (described abve)
    /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/i18n/*. Jar
    $ Catalina_home/common/lib/*. Jar
    $ Catalina_base/shared/classes
    $ Catalina_base/shared/lib/*. Jar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.