Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining Tomcat servlet context parameters

Tags:

tomcat6

Scenario:

foo.war file contains a default value of init parameter fooParam=1.

This is defined in foo.war!WEB_INF/web.xml which contains:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app 
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

    <context-param>
      <param-name>fooParam</param-name>
      <param-value>1</param-value>
      <description>
        my parameter "fooParam"
      </description>
    </context-param>

    ...

OK, now I want to be able to override it in a config file in the Tomcat {$CATALINA_HOME}/conf/ directory. Where/how can I do this???

like image 923
Jason S Avatar asked Oct 26 '09 17:10

Jason S


People also ask

Where is context defined in Tomcat?

A Context element contained within a Host element in the main "conf/server. xml" file. This technique allows Contexts to be defined in the central server configuration file. This technique was common through Tomcat 4. x, but is deprecated as of Tomcat 5.

What is context param in servlet?

The context-param element, subelement of web-app, is used to define the initialization parameter in the application scope. The param-name and param-value are the sub-elements of the context-param. The param-name element defines parameter name and and param-value defines its value.


1 Answers

According to the documentation of the Context element:

Context Parameters

You can configure named values that will be made visible to the web application as servlet context initialization parameters by nesting <Parameter> elements inside this element. For example, you can create an initialization parameter like this:

<Context ...>
  ...
  <Parameter name="companyName" value="My Company, Incorporated"
         override="false"/>
  ...
</Context>

This is equivalent to the inclusion of the following element in the web application deployment descriptor (/WEB-INF/web.xml):

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

but does not require modification of the deployment descriptor to customize this value.

The valid attributes for a <Parameter> element are as follows:

...

About the override attribute of a <Parameter>, the documentation says:

Set this to false if you do not want a <context-param> for the same parameter name, found in the web application deployment descriptor, to override the value specified here. By default, overrides are allowed.

Setting it to false should do the trick. This was the "how" part.


For the "where" part, read refer to the introduction of The Context Container:

For Tomcat 6, unlike Tomcat 4.x, it is NOT recommended to place <Context> elements directly in the server.xml file. This is because it makes modifing the Context configuration more invasive since the main conf/server.xml file cannot be reloaded without restarting Tomcat.

Context elements may be explicitly defined:

  • In the $CATALINA_BASE/conf/context.xml file: the Context element information will be loaded by all webapps.
  • In the $CATALINA_BASE/conf/[enginename]/[hostname]/context.xml.default file: the Context element information will be loaded by all webapps of that host.
  • In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The name of the file (less the .xml extension) will be used as the context path. Multi-level context paths may be defined using #, e.g. foo#bar.xml for a context path of /foo/bar. The default web application may be defined by using a file called ROOT.xml.
  • Only if a context file does not exist for the application in the $CATALINA_BASE/conf/[enginename]/[hostname]/, in an individual file at /META-INF/context.xml inside the application files. If the web application is packaged as a WAR then /META-INF/context.xml will be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to match the application's context path. Once this file exists, it will not be replaced if a new WAR with a newer /META-INF/context.xml is placed in the host's appBase.
  • Inside a Host element in the main conf/server.xml.

With the exception of server.xml, files that define Context elements may only define a single Context element.

like image 104
Pascal Thivent Avatar answered Oct 23 '22 07:10

Pascal Thivent