Configure Tomcat application context root with context.xml 

There are 3 notes for this topic, click above title to see all notes.
Joined:
04/09/2007
Posts:
773

February 15, 2010 06:09:56    Last update: February 16, 2010 03:08:34
The context root of a web application determines the root path of URLs that will be handled by that application. For example, if the context root is example, then URLs starting with /example (i.e., /example/*) will be handled by that application.

By default, Tomcat uses the WAR file name (without the .war extension) or, if deployed in exploded directory form, the name of the top level directory as the context root. For example, the pre-installed examples application is deployed under the examples directory under webapps, and its context root is examples.

You may want to use a different context root than Tomcat's default. For example, if you build your application with Maven, the resulting WAR file might be named my-fabulous-app-1.0-SNAPSHOT.war, but you don't want 1.0-SNAPSHOT as part of your context root. As of Tomcat 6, the only way to configure a context root that is different than the WAR or directory name, dispite Tomcat's warning against it, is to add a Context element inside Host in $CATALINA_BASE/conf/server.xml:
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
	<Context path="/my-fabulous-app" docBase="my-fabulous-app-1.0-SNAPSHOT"/>
    </Host>


With the path attribute specified, both /my-fabulous-app and /my-fabulous-app-1.0-SNAPSHOT point to the same application.

You might think that creating a context.xml under META-INF of the WAR file would also work, it doesn't. The path attribute is ignored unless specified in server.xml.
Share |
| Comment  | Tags