A detailed description of the context container configuration in Tomcat

Source: Internet
Author: User
Tags http request tomcat
The context element represents a Web application that runs on a specific virtual host. As described in servlet specification 2.2 or later, each Web app is based on a Web application Archive (War) file, or a directory that contains the extracted contents of the WAR file. For more information about Web application archive, you can refer to the servlet specification and Tomcat application Developer's Guide.
By matching the maximum possible prefix of the request URI to the context path of each context, Catalina selects the appropriate Web app to process the HTTP request. Once selected, the context selects a correct servlet to process incoming requests, based on the servlet mappings defined in the Web application deployment descriptor file. The servlet mappings must be defined in the/web-inf/web.xml of the WEB app directory hierarchy.

You can nest any number of context elements in a host element. The path for each context must be unique and defined by the Path property. In addition, you must define a context with a context path length of 0, which is called the virtual host's default Web application to handle requests that do not match the context path of any context.

In addition to nesting the context elements in the host element, you can store them in a single file (with an. xml suffix), placed under the $catalina_home/conf/[enginename]/[hostname]/directory. For more information on this, refer to the automatic release of your app. This approach allows the Web app to be reconfigured dynamically, because if Conf/server.xml is modified, Tomcat must be restarted.

In addition to explicitly specifying the context element, there are several other techniques that can automatically generate a context element. Refer to Automatic application Deployment and User Web applications

The following description uses the variable $catalina_home to refer to the directory that Tomcat 5 installs. Most of the relative paths are based on that directory. However, you can run multiple Tomcat 5 instances by setting up the Catalina_base directory, and you should use $catalina_base as the directory baseline instead of using $catalina_home.

Property
Common Properties
All context implementations support the following properties:
Backgroundprocessordelay This value represents the delay, in seconds, of calling the Backgroundprocess method on the context and its child containers (including all wrappers). If the delay value is non-negative, the child container is not called, which means that the child container uses its own processing thread. If the value is positive, a new thread is created. After waiting for a specified time, the thread calls the Backgroundprocess method on the host and its child containers. The context uses the background processing session to expire and monitor class changes for re-loading. If not specified, the default value of this property is-1, which indicates that the context relies on the background processing of the host to which it belongs.
The Java class name implemented by the ClassName. The class must implement the Org.apache.catalina.Context interface. If not specified, the standard implementation is used (defined below).
Cookies are set to ture if they wish to use cookies to pass session identifier (client support cookies are required). Otherwise false, this case can only rely on the URL rewriting pass session identifier.



Crosscontext If you want to call Servletcontext.getcontext () within the app to return the request dispatcher of other Web application running on that virtual host, set to True. In a security-critical environment, set to False so that getcontext () always returns NULL. The default value is False.

DocBase the document base directory for the Web app, also known as context Root, or the path to the war file. You can use an absolute path, or you can use the AppBase path relative to the host that the context belongs to.



Override set to True if you want to overwrite the corresponding setting in Defaultcontext with the settings in the context element. The settings in Defaultcontext are used by default.

Privileged is set to true to allow the context to use container servlets, such as the manager servlet.



The context path of the path Web App. Catalina compares the start of each URL to the context path and selects the appropriate Web app to process the request. The context path under a specific host must be unique. If the context path is an empty string (""), this context is the default Web application of the owning host to handle requests that do not match any of the context path.



reloadable If you want Catalina to monitor/web-inf/classes/and/web-inf/lib The following classes are changed, automatically reload the WEB application when changes occur, set to true. This feature is useful in the development phase, but it also greatly increases the overhead of the server. Therefore, it is not recommended to use it after publishing. However, you can use the Manager app to trigger overloads of the app when necessary.
The name of the Wrapperclass Org.apache.catalina.Wrapper implementation class for the servlets of the context management. If not specified, the standard default value is used.

Standard implementations
The standard implementation of the context is org.apache.catalina.core.StandardContext. It also supports the following additional properties:

Property Description
Debug the verbosity of debugging information that is associated with this engine with the logger record. The larger the number, the more detailed the output. If not specified, the default is 0.



Swallowoutput If the value is true,system.out and the output of System.err is redirected to the Web app's logger. If not specified, the default value is False

Usenaming set to True if you want Catalina to enable a Jndi InitialContext object for the Web App. The initialialcontext conforms to the agreement of the Java EE platform and the default value is true.

Workdir the path to the temporary directory provided by the context for the temporary read/write of the servlet. With the Javax.servlet.context.tempdir property, the servlet can access the directory. If not specified, use $catalina_home/work to the next appropriate directory.



Nested components
The following elements can be nested within a context element, but can only be nested once per element.

Loader-configures the ClassLoader that the Web app uses to load Servlets and JavaBean. Under normal circumstances, using the default classloader is sufficient;
Logger-Configures the Logger that is used to receive and process all log messages, including all messages that are logged by the call to the ServletContext.log () function;
Manager-configures session manager for creating, destroying, and maintaining HTTP sessions. Normally, using the default Session manager configuration is sufficient;
Realm-Configure realm, the realm's user database and related roles are used only in this particular Web application. If not specified, the Web app uses the realm of the host or engine it belongs to.
Resources-configured to access the static resources associated with this web app. Under normal circumstances, using the default resource manager is sufficient.

Proprietary features
Access log
Under normal circumstances, running the Web server generates an access log. The access log outputs one line of information for each request in a standard format. The Catalina includes an optional valve implementation that generates logs in a standard format and can be used in any custom format.

By nesting a valve element in a engine,host or context, Catalina creates an access log for all requests processed by that container, as follows:


<context path= "/examples" ...>
...
<valve classname= "Org.apache.catalina.valves.AccessLogValve"
prefix= "Localhost_access_log" suffix= ". txt"
pattern= "Common"/>
...
</Context>



Refer to access Log Valve for more information on configuration properties;

Automatic configuration of the context
If a standard context implementation is used, when the Catalina is started, or when the Web application is overloaded, the following configuration steps occur automatically, and no special configuration is required to enable this feature.

If you do not define your own loader element, a standard Web application class loader will be configured;
If you do not define your own manager element, a standard session manager is configured;
If you do not define your own resources element, use the standard resource manager.
The properties of the Web app listed in Conf/web.xml are treated as default properties for that Web App. This is used to establish default mappings (such as mapping. JSP to the corresponding JSP servlet), as well as other standard properties.
The attributes enumerated in the/web-inf/web.xml resource are processed (if the resource exists);
If the Web app specifies a security limit, and the user may need to be authenticated, Catalina configures the selected authenticator, which implements the login method.

Context parameter
You can nest elements in the context <Parameter> elements and configure values with names that are ServletContext initialization parameters that are visible to the entire Web application. For example, you can create initialization parameters like this:


<context ...>
...
<parameter name= "CompanyName" value= "My Company, Incorporated"
Override= "false"/>
...
</Context>



This is equal to the inclusion of the following elements in/web-inf/web.xml:


<context-param>
<param-name>companyName</param-name>
<param-value>my Company, Incorporated</param-value>
</context-param>



The difference is that the former does not need to modify deployment descriptor to customize this value.

The valid attribute values for the <Parameter> element are as follows:

Property Description
Description text Description of the context initialization parameter (optional)

Name









The name of the context initialization parameter to create

Override if you do not want the/web-inf/web.xml to have the same parameter name in the <context-param> override the value specified here, set to false. The default value is true.

When value is called Servletcontext.getinitparameter (), the parameter value returned to the application.


Environment articles
You can nest <Environment> elements in the context and configure named values that are visible to the entire web app as an Environment entry resource (Environment Entry Resource). For example, you can create an environment entry as follows:


<context ...>
...
<environment name= "maxexemptions" value= "10"
Type= "Java.lang.Integer" override= "false"/>
...
</Context>



This is equivalent to the inclusion of the following elements in/web-inf/web.xml:


<env-entry>
<env-entry-name>maxExemptions</param-name>
<env-entry-value>10</env-entry-value>
<env-entry-type>java.lang.Integer</env-entry-type>
</env-entry>



The difference is that the former does not need to modify deployment descriptor to customize this value.

The valid attributes of the <Environment> element are as follows:

Property Description
Text description of the description environment entry (optional)

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.