[ Team LiB ] Previous Section Next Section

Recipe 2.2 Using a Context Element in Tomcat'sserver.xml

Problem

You want to deploy and redeploy a servlet on Tomcat 4.1.x without restarting the Tomcat web container.

Solution

Deploy the servlet as part of a Context element in Tomcat's server.xml file.

Discussion

You can paste a recompiled servlet class over an existing servlet class and invoke the servlet without restarting Tomcat:

  1. Locate the Context element for your web application or create a new Context element in the <tomcat-installation-directory>/conf/server.xml file. Context elements must be nested within the Host element that represents the virtual host under which your web application is running.

  2. Set the reloadable attribute of your Context element to true. This signals Tomcat to monitor the contents of WEB-INF/classes and WEB-INF/lib for any changes. If changes are detected, Tomcat automatically reloads the web application.

The Context element in server.xml looks like this:

<Context className="org.apache.catalina.core.StandardContext" 
         crossContext="false" reloadable="true" 
         mapperClass="org.apache.catalina.core.StandardContextMapper" 
         useNaming="true" debug="0" swallowOutput="false" 
         privileged="false" displayName="Home Web App" 
         wrapperClass="org.apache.catalina.core.StandardWrapper" 
         docBase="h:\home" cookies="true" path="/home" 
         cachingAllowed="true" 
         charsetMapperClass="org.apache.catalina.util.CharsetMapper"
>

The path attribute represents the context path for the application. The docBase attribute points to the directory that represents the top level of this web application. Most of the example's other attributes have values that are shared by other Contexts. For example, cookies="true" indicates that the Context will use cookies for the session identifier, and crossContext="false" prevents the servlets in this web application from obtaining request dispatchers for other web applications running in the virtual host.

Setting the reloadable attribute to true incurs significant runtime overhead, so this configuration is recommended only for web applications in development mode.


Under this configuration, Tomcat 4.1.x displays a console message after a slight delay when you paste a new servlet class over the old one in the web application. Here is an example of a console message in response to a dynamic servlet reload:

WebappClassLoader:   Resource '/WEB-INF/classes/com/jspservletcookbook/OracleTest.
class' was modified; 
Date is now: Sun Feb 02 22:17:41 EST 2003 Was: Sun Feb 02 21:38:52 EST 2003

See Also

The deployment sections of Tomcat: The Definitive Guide, by Brittain and Darwin (O'Reilly); Recipe 2.1, Recipe 2.4, and Recipe 2.6; Jakarta Tomcat documentation for the Context element: http://jakarta.apache.org/tomcat/tomcat-4.1-doc/config/context.html.

    [ Team LiB ] Previous Section Next Section