Configure Tomcat application context root with context.xml 

Displaying notes 1 - 3 of 3

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
 
Joined:
03/05/2012
Posts:
1

March 05, 2012 13:36:41    Last update: March 05, 2012 13:36:41
Thanks Dr.Xi, I was having a tough time changing the deployment context path of my web-app using the
so-called context.xml file under /META-INF, it dint work (tomcat documentation at http://tomcat.apache.org/tomcat-6.0-doc/config/context.html , seemed pretty sure it would :()

But I have one question, as changing server.xml is too instrusive for every web-app you build, is there any other way of pushing this configuration to one of the application specific files ?

thanks,
Venky
Share |
| Comment  | Tags
 
jpro
Joined:
04/09/2007
Posts:
773

March 08, 2012 09:39:22    Last update: March 08, 2012 09:39:22
Actually there is an easy solution: change the name of the war file at deployment!

Sample ant task:
<target name="deploy" depends="package">
    <copy file="${target.war}" tofile="${tomcat.webapps}/${deploy.war}"/>
</target>
Share |
| Comment  | Tags