How Tomcat works Reading Notes under 8 Loaders

Source: Internet
Author: User
Let's take a look at the previous articles. This section starts with simplewrapper's loadservlet.
Simplewrapper. Java is as follows (try catch and other code are omitted)
public Servlet loadServlet() throws ServletException {     ...    String actualClass = servletClass;    Loader loader = getLoader();    // Acquire an instance of the class loader to be used    if (loader==null) {      throw new ServletException("No loader.");    }    ClassLoader classLoader = loader.getClassLoader();     if (classLoader!=null) {        classClass = classLoader.loadClass(actualClass);         // Instantiate and initialize an instance of the servlet class itself      servlet = (Servlet) classClass.newInstance();       // Call the initialization method of this servlet      servlet.init(null);       return servlet;  }


We know that loader. getclassloader (); The returned webappclassloader class
The key point is classloader. loadclass (actualclass );
Classloader. loadclass (actualclass) finally calls the following method, and resolve is false;

The method below is very long, but in summary it is just a few steps

· All loaded classes must be cached. Therefore, you must first check the local cache.
· If you cannot find classes in the local cache, use the findloaderclass method of the Java. langclassloader class to find classes in the cache,
· If the class cannot be found in both caches, use the system class loader to avoid overwriting Web applications from J2EE classes.
· If the security manager is used, check whether the class can be loaded. If the class cannot be loaded, A classnotfoundexception exception is thrown.
· If the class to be loaded uses a delegate flag or the class belongs to the trigger package, use the parent loader to load the class. If the parent loader is null, use the system loader to load the class.
· Load classes from the current source
· If the class cannot be found in the current source and the delegation flag is not used, use the parent class loader. If the parent class loader is null, use the system loader.

· If the class still cannot be found, a classnotfoundexception exception is thrown.


Public class <?> Loadclass (string name, Boolean resolve) throws classnotfoundexception {If (debug> = 2) log ("loadclass (" + name + "," + resolve + ")"); class <?> Clazz = NULL; // don't load classes if Class Loader is stopped if (! Started) {// If webappclassloader is not started .... log ("lifecycle error: CL stopped"); throw new classnotfoundexception (name);} // (0) Check our previusly loaded local class cache clazz = findloadedclass0 (name ); // check the Code temporarily put in below, so first find if (clazz! = NULL) {If (debug> = 3) log ("returning class from cache"); If (RESOLVE) resolveclass (clazz); Return (clazz );} /* protected class <?> Findloadedclass0 (string name) {resourceentry entry = (resourceentry) resourceentries. Get (name); If (entry! = NULL) {return entry. loadedclass;} return (null);} * // (0.1) Check our previusly loaded class cache clazz = findloadedclass (name); // go to the virtual machine to find if (clazz! = NULL) {If (debug> = 3) log ("returning class from cache"); If (RESOLVE) resolveclass (clazz); Return (clazz );} // (0.2) Try loading the class with the system class loader, to prevent // The webapp from overriding j2se classes try {// system is the system class loader sun. misc. launcher $ appclassloader // first load with the system loader // Why do I first use the system loader? If you have written the full name of a class as "Java. lang. object "Understand? // You may wonder if the system loader can load anything? What should I do with the code? // What is the directory for loading the system loader? You can see that clazz = system. loadclass (name); If (clazz! = NULL) {If (RESOLVE) resolveclass (clazz); Return (clazz) ;}} catch (classnotfoundexception E) {// ignore} // I have not studied the specific code in the security question. // (0.5) permission to access this class when using a securitymanager if (securitymanager! = NULL) {int I = Name. lastindexof ('. '); if (I> = 0) {try {securitymanager. checkpackageaccess (name. substring (0, I);} catch (securityexception SE) {string error = "security violation, attempt to use" + "restricted class:" + name; system. out. println (error); se. printstacktrace (); log (error); throw new classnotfoundexception (error) ;}}// the default value of delegate is false // filter. If the class to be loaded is defined in packagetrigers Return true // The book says that the package in packagetrigers is not allowed to be loaded // the package in the Code does not reflect that it cannot be loaded? Who knows why? Please let me know Boolean delegateload = delegate | filter (name); system. out. println (delegate + "" + filter (name) + "DDD"); // (1) delegate to our parent if requested if (delegateload) {If (debug> = 3) log ("delegating to parent classloader"); classloader loader = parent; system. out. println (loader + "loader"); If (loader = NULL) loader = system; try {clazz = loader. loadclass (name); If (clazz! = NULL) {If (debug> = 3) log ("loading class from parent"); If (RESOLVE) resolveclass (clazz); Return (clazz );}} catch (classnotfoundexception e) {;}// (2) Search Local repositories // As for the repositories attribute, the setrepositories () set the methods to use as follows // findclass findclassinternal findresourceinternal // load files in WEB-INF/classes first in findresourceinternal, then load jar files under WEB-INF/lib // The Last resourceen Tries. put (name, entry); // puts the loaded enety into resourceentries and caches it if (debug> = 3) log ("Searching Local repositories "); try {clazz = findclass (name); If (clazz! = NULL) {If (debug> = 3) log ("loading class from local repository"); If (RESOLVE) resolveclass (clazz); Return (clazz );}} catch (classnotfoundexception e) {;} // The system loader has been loaded before it. If the program can run here, it indicates that the system loader is not working. Why should we load it again? // (3) delegate to parent unconditionally if (! Delegateload) {If (debug> = 3) log ("delegating to parent classloader"); classloader loader = parent; If (loader = NULL) loader = system; try {clazz = loader. loadclass (name); If (clazz! = NULL) {If (debug> = 3) log ("loading class from parent"); If (RESOLVE) resolveclass (clazz); Return (clazz );}} catch (classnotfoundexception e) {;}// this class was not found throw new classnotfoundexception (name );}

The application has already said in the previous section that in order to automatically reload containers after file changes, you need to configure
<Context Path = "/MyApp" docbase = "MyApp"/>
Now we do not have this configuration file. Instead, add the following code to the Startup Program:
Context context = new StandardContext();// StandardContext's start method adds a default mappercontext.setPath("/myApp");context.setDocBase("myApp");


If the above Code runs in eclipse, A MyApp folder and work folder will be generated under the project root directory.
In the MyApp folder, place the class files of the two servlets used in the previous blogs in the Set WEB-INF/classes folder


In addition, the method of auto-Reload is to compare with the last modification time. For more information about the code, see the source code.

How Tomcat works Reading Notes under 8 Loaders

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.