Configure Tomcat application context root with context.xml 

Displaying notes 1 - 6 of 6

Joined:
04/09/2007
Posts:
776

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
1 comment  
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:
776

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
 
Joined:
05/14/2014
Posts:
1

May 14, 2014 07:59:40    Last update: May 14, 2014 07:59:40
As you have mentioned above, i have changed and its working fine.
But my doubt here is,

Assume day one you have deployed release abc-RELEASE-0.0.1.war. assume on day 2 you want to deploy second release abc-RELEASE-0.0.2.war.

if we hard code the war file name in <Context>, the each and every time when the war file name changes like abc-RELEASE-0.0.3,0.0.4 as per the release then, each and every time we need to change the value at <Context> tag in server.xml file. I want it to be configured only once irrespective of the release As we do in JBOSS.
could you please help me in this..
Share |
| Comment  | Tags
 
Joined:
04/09/2007
Posts:
776

June 04, 2014 18:59:37    Last update: June 04, 2014 18:59:37
There's really no good solution since Tomcat hardwires the root context to the WAR file name. The easiest solution might be simply rename the WAR file during deployment.
Share |
| Comment  | Tags
 
Joined:
07/29/2014
Posts:
2

July 29, 2014 10:35:51    Last update: July 29, 2014 10:35:51
For working with xml files use a good xml tool like an xml editor.
Share |
| Comment  | Tags