Tomcat4 ClassLoader How To

xiaoxiao2021-03-05  21

The Tomcat 4 servlet / jsp ContainerClass Loader How-Toprint-Friendlyversion

Quick Start

The Following Rules Cover About 95% of The Decisions That Application Developers and Deployers Must Make About Where to Place Class and Resource Files To Make Available To Web Applications:

For classes and resources specific to a particular 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 . For classes and resources that must be shared across 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.

Overview

Like many server applications, Tomcat 4 installs a variety of class loaders (that is, classes that implement java.lang.ClassLoader) to allow different portions of the container, and the web applications running on the container, to have access to different repositories of Available Classes and Resources. This Mechanism IS Used to Provide The FunctionAlity Defined In The Servlet Specification, Version 2.3 - In Particular, Sections 9.4 and 9.6.

In a Java 2 (that is, JDK 1.2 or later) environment, class loaders are arranged in a parent-child tree. Normally, when a class loader is asked to load a particular class or resource, it delegates the request to a parent class loader first, and looks in its own repositories only if the parent class loader (s) can not find the requested class or resource. The model for web application class loaders differs slightly from this, as discussed below, but the main principles are the same then .

WHEN Tomcat 4 Is Started, IT Creates A Set of Class Loaders That Area Organized Into The Following Parent-Child Relationships, Where The Parent Class Loader IS Above The Child Class Loader: Bootstrap

|

SYSTEM

|

Common

/ /

Catalina Shared

/ /

WebApp1 WebApp2 ...

THE CHARACTERISTICS OF Each of these Class Loaders, include the source of classes and resources That The make Visible, Are Discussed in detail in The Following Section.

Class Loader definitions

As Indicated in The Diagram Above, Tomcat 4 Creates The Following Class Loaders as it is initialized:

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 4 startup scripts ($ CATALINA_HOME / bin / catalina.sh or% CATALINA_HOME% / bin / catalina.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 4 server, and the class loader implementation classes it depends on $ JAVA_HOME / lib / tools.jar -. Contains the "javac" compiler used to convert JSP pages into servlet classes 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.. / common / classes, as well as classes and resources in JAR files under the $ CATALINA_HOME / commons / endorsed and $ CATALINA_HOME / common / lib directories, are made visible through this class loader By default, that includes the following.:

jndi.jar - The Java Naming and Directory Interface API classes (loaded ONLY on a JDK 1.2 system, because they are included automatically on JDK 1.3 and later) naming-common.jar -. The JNDI implementation used by Tomcat 4 to represent in- memory naming contexts naming-resources.jar -. The specialized JNDI naming context implementation used to represent the static resources of a web application servlet.jar -. The Servlet and JSP API classes xerces.jar -. The XML parser that is visible by default to Tomcat internal classes and to web applications This can be overridden, for a particular web application, by including your desired parser in / WEB-INF / lib Catalina -.. This class loader is initialized to include all classes and resources required to implement Tomcat 4 itself. These classes and resources are TOTALLY invisible to web applications. All unpacked classes and resources in $ CATALINA_HOME / server / classes, as well as classes and resources in JAR files under $ CATALINA_HOME / server / lib, are Made Visible THROUGH THIS Class Loader. by Default, That Includes the fol

catalina.jar - Implementation of the Catalina servlet container portion of Tomcat 4. jakarta-regexp-XYjar - The binary distribution of the Jakarta Regexp regular expression processing library, used in the implementation of request filters servlets-xxxxx.jar -. The classes associated with each internal servlet that provides part of Tomcat's functionality These are separated so that they can be completely removed if the corresponding service is not required, or they can be subject to specialized security manager permissions tomcat-coyote.jar -.. Coyote connector for Tomcat 4. tomcat-http11.jar - Standalone Java HTTP / 1.1 connector tomcat-jk.jar -. Classes for the Java portion of the JK web server connector, which allows Tomcat to run behind web servers such as Apache and iPlanet iAS and iWS Tomcat-jk2.jar - Classes for the Java Portion of the JK 2 Web Server Connector, Which Allows Tomcat to Run BEHIND Web Servers Such As Apache and Iplanet IAS AND IWS. Tomcat-Util.jar - Utility Classe s required by some Tomcat connectors tomcat-warp.jar -. Classes for the Java portion of the Webapp web server connector, using the WARP protocol which allows Tomcat to run behind the Apache HTTPD web server (1.3 and 2.0) Shared -. This class loader is the place to put classes and resources that you wish to share across ALL web applications (unless Tomcat internal classes also need access, in which case you should put them in the Common class loader instead). All unpacked classes and resources in $ CATALINA_BASE / Shared / Classes, AS Well AS Classes And Resources In Jar Files Under $ Catalina_Base / Shared / Lib, Are Made Visible Thros Class Loader. by Default, That Includes the Following:

jasper-compiler.jar - The page compiler classes required to convert JSP source pages into executable servlets and compile them jasper-runtime.jar -. The runtime support classes required to execute JSP pages that have already been translated into Java servlets and then compiled. naming-factory.jar - JNDI object factories for resources supported by the default JNDI naming context provided to web applications WebappX -.. A class loader is created for each web application that is deployed in a single Tomcat 4 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. AS 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.6). WHE n 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. All other class loaders in Tomcat 4 follow the usual delegation pattern.

Therefore, from The Perspective of a Web Application, Class or Resource Loading Looks in The Following Repositories, in this Order:

/ WEB-INF / classes of your web application /WEB-INF/lib/*.jar of your web application Bootstrap classes of your JVM System class loader classses (described above) $ CATALINA_HOME / common / classes $ CATALINA_HOME / common / endorsed / * .jar $ catalina_home / common / lib / *. Jar $ catalina_base / shared / class $ catalina_base / shared / lib / *. jar

J2SE Classes and Extensions

In addition to the previous rules, the web application classloader delegates classloading for classes which are part of J2SE (which means it will attempt loading first with its parent classloader before loading from its current repositories, regardless of the current delegation setting). This includes classes From The Sax and Dom APIS, Xalan, Xerces, And Any Classes Which Package Name Starts with "javax.".

The Web Application ClassLoader Will Also Refuse To Load Classes from The Core Java Language ("java. *" Packages).

XML Parsers and JDK 1.4

.

In previous versions of Tomcat 4, you could simply replace the XML parser in the $ CATALINA_HOME / common / lib directory to change the parser used by all web applications. However, this technique will not be effective when you are running on JDK 1.4, because ...........

. JDK 1.4 supports a mechanism called the "Endorsed Standards Override Mechanism" to allow replacement of APIs created outside of the JCP (ie DOM and SAX from W3C) It can also be used to update the XML parser implementation For more information, see.: Http://java.sun.com/j2se/1.4/docs/guide/standards/index.html.

Tomcat utilizes this mechanism by including the system property setting -Djava.endorsed.dirs = $ CATALINA_HOME / common / endorsed in the command line that starts the container. Therefore, you can replace the parser that is installed in this directory, and it will get Used Even ON A JDK 1.4 System.

Running Under a Security Manager

WHEN Running Under A Security Manager The Locations from Which Classes Are Permitted to Be loaded Will Also Depend On The Contents of Your Policy File. See Security Manager How-to for Further Information.

转载请注明原文地址:https://www.9cbs.com/read-33247.html

New Post(0)