Tomcat Kernel Tomcat class loader

Source: Internet
Author: User

Like other mainstream Java Web servers, Tomcat has a different custom classloader to control the various repositories. In general, the Java Web server needs to address the following four issues:

① in the same Web server, the Java class libraries used by each Web project are isolated from each other.

② in the same Web server, a shared Java class Library can be provided between individual Web projects.

③ servers in order to be unaffected by Web projects, the server's class library should be independent of the application's class library.

④ for JSP-enabled Web servers, the hot-swap (HotSwap) feature should be supported.

For the above issues, if using a class loader alone is obviously not effective, you must use several custom class loaders based on the actual usage.

The following is the main analysis of the book TOMCAT7 as an example to see how its class loader is defined? 2-4-3, launch ClassLoader, extension classloader, Application ClassLoader, three ClassLoader-level loaders, they are unique, and we generally do not make any changes to them.  Next is the Tomcat class loader, in Tomcat7, the most important class loader is common ClassLoader, its parent classloader is applicationclassloader, responsible for loading $CATALINA _base/lib, $CATALINA _home/lib all the. class and. jar files in two directories. The two class loaders below the dashed box are necessary to illustrate that, if the two ClassLoader instances are different by default from the Common ClassLoader instances in the TOMCAT5 version, Common ClassLoader is their parent classloader. In Tomcat7, these two instance variables are also present, Just catalina.properties configuration file does not configure Server.loader and Share.loader two items, so in the program, the two ClassLoader instances are assigned to the Commonclassloader instance, that is, a Tomcat instance is actually only com Monclassloader instance. As the following code

private void initclassloaders () {

Try {

Commonloader = Createclassloader ("Common",null);

if (commonloader = = null ) {

Commonloader=this. getclass (). getClassLoader ();

}

Catalinaloader = Createclassloader ("Server",commonloader);

Sharedloader = Createclassloader ("Shared",commonloader);

} catch (Throwable t) {

handlethrowable (t);

log. Error ("Class loader creation threwexception", T);

System. Exit (1);

}

}

First create a commonloader, and then pass the Commonloader as a parameter into the Createclassloader method, In this method, depending on whether the Server.loader and Share.loader properties in the catalina.properties are empty, the new class loader is created, and if the property is empty, the Commonloader is assigned directly to Catalinaloader and S Haredloader. If the default configuration does not meet your needs, you can modify the Catalina.properties profile to meet your needs. WebappClassLoader from the name of the general knowledge of the main load of the Web application, its parent class loader is common ClassLoader, generally have multiple WebApp class loader instances, each ClassLoader load a Web program, loading path is/ Webapp/web-inf directory. Finally, JSP ClassLoader is responsible for loading the JSP file compiled Class,webapp ClassLoader as its parent class loader, when Tomcat detects that the JSP file has changed, will create a new JSP ClassLoader and replace the current JSP Classlassloader, the JSP in the/webapp/web-inf directory to load.

Figure 2-4-3 TOMCAT7 Class Loader

Check the structure of such a classloader to see if the Java Web server above solves the problem. Because each web App project has its own WebApp ClassLoader, it's good to isolate multiple Web applications from each other and effectively make Tomcat unaffected by Web applications Common The existence of ClassLoader enables multiple Web applications to share class libraries with each other, and each JSP file corresponds to a JSP classloader that enables Tomcat to support hot-swap functionality.


Tomcat Kernel Tomcat class loader

Related Article

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.