问题描述
在《How tomcat works》这本书中说道:“servlet不能访问其他的类,即使这些类被包含在运行着当前tomcat的jvm的CLASSPATH中。servlet只能访问WEB-INF/lib目录下的类库。 我写了个servlet试了一下,在servlet能访问到其他的类呀,这句话到底怎么理解呀,一直理解不了?
解决方案
Servlet只允许访问WEB-INF/目录及其子目录下面的类以及部署在WEB-INF/lib目录下的类库。所以一个servlet容器需要一个自己的加载器,该加载器遵守一些特定的规则来加载类。在Catalina中,加载器使用org.apache.catalina.Loader接口表示。估计是这样的 如我们有两个web应用a 和 b a是访问不到b的 但可以访问父加载器的(如tomcat/lib下的)类加载器的问题。Bootstrap - This class loader contains the basic runtime classes provided by the Java Virtual Machine, plus any classes from JAR files present in the System Extensions directory ($JAVA_HOME/jre/lib/ext). NOTE - Some JVMs may implement this as more than one class loader, or it may not be visible (as a class loader) at all.System - This class loader is normally initialized from the contents of the CLASSPATH environment variable. All such classes are visible to both Tomcat internal classes, and to web applications. However, the standard Tomcat 6 startup scripts ($CATALINA_HOME/bin/catalina.sh or%CATALINA_HOME%bincatalina.bat) totally ignore the contents of the CLASSPATH environment variable itself, and instead build the System class loader from the following repositories:$CATALINA_HOME/bin/bootstrap.jar - Contains the main() method that is used to initialize the Tomcat 6 server, and the class loader implementation classes it depends on.$CATALINA_HOME/bin/tomcat-juli.jar - Package renamed Jakarta commons logging API, and java.util.logging LogManager.Common - This class loader contains additional classes that are made visible to both Tomcat internal classes and to all web applications. Normally, application classes should NOT be placed here. All unpacked classes and resources in $CATALINA_HOME/lib, as well as classes and resources in JAR files are made visible through this class loader. By default, that includes the following:annotations-api.jar - JEE annotations classes.catalina.jar - Implementation of the Catalina servlet container portion of Tomcat 6.catalina-ant.jar - Tomcat Catalina Ant tasks.catalina-ha.jar - High availability package.catalina-tribes.jar - Group communication package.el-api.jar - EL 2.1 API.jasper.jar - Jasper 2 Compiler and Runtime.jasper-el.jar - Jasper 2 EL implementation.jasper-jdt.jar - Eclipse JDT 3.2 Java compiler.jsp-api.jar - JSP 2.1 API.servlet-api.jar - Servlet 2.5 API.……WebappX - A class loader is created for each web application that is deployed in a single Tomcat 6 instance. All unpacked classes and resources in the /WEB-INF/classes directory of your web application archive, plus classes and resources in JAR files under the /WEB-INF/lib directory of your web application archive, are made visible to the containing web application, but to no others.Therefore, from the perspective of a web application, class or resource loading looks in the following repositories, in this order:Bootstrap classes of your JVMSystem class loader classses (described above)/WEB-INF/classes of your web application/WEB-INF/lib/*.jar of your web application$CATALINA_HOME/lib$CATALINA_HOME/lib/*.jarAs mentioned above, the web application class loader diverges from the default Java 2 delegation model (in accordance with the recommendations in the Servlet Specification, version 2.3, 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. There are exceptions. Classes which are part of the JRE base classes cannot be overriden.For some classes (such as the XML parser components in J2SE 1.4+), the J2SE 1.4 endorsed feature can be used. Last, any JAR containing servlet API classes will be ignored by the classloader. All other class loaders in Tomcat 6 follow the usual delegation pattern.
解决方案二:
http://sishuok.com/forum/blogPost/list/0/4120.html