谁加载了我 – 2.Tomcat6里面的web应用

public class MyServlet extends HttpServlet {
 

	protected void doGet(HttpServletRequest req, HttpServletResponse response)
			throws ServletException, IOException {
		 

		print(Servlet.class.getClassLoader());  //输出org.apache.catalina.loader.StandardClassLoader,它负责加载/common下的类库; Servlet.class就是common/servlet-api.jar里的类

		print(HttpServlet.class.getClassLoader()); //同上 
		//看看它的父加载器
		print(HttpServlet.class.getClassLoader().getParent()); //是sun.misc.Launcher$AppClassLoader,即system classloader

		
		print(MyServlet.class.getClassLoader());//输出org.apache.catalina.loader.WebappClassLoader,它负责加载当前web-app下的类库; MyServlet.class就是当前web-app下的类
		//看看它的父加载器
		print(MyServlet.class.getClassLoader().getParent()); //输出 org.apache.catalina.loader.StandardClassLoader
		
		 
		//试着找一下 Tomcat启动入口类的加载器 
		print(this.getClass().getClassLoader().loadClass("org.apache.catalina.startup.Bootstrap").getClassLoader());  //输出sun.misc.Launcher$AppClassLoader,即system classloader
		 

	//看看当前的context class loader是哪个		
        print(Thread.currentThread().getContextClassLoader()); //输出org.apache.catalina.loader.WebappClassLoader,即加载当前web-app的加载器	 
    }
}

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.