Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify path in META-INF/context.xml for Tomcat

Tags:

tomcat

tomcat7

I'm using Tomcat 7 and would like to set the context root of a war file in the war file itself and have Tomcat autodeploy and pick up this path. I thought I found the way to do it by putting a context.xml in the META-INF directory of the war which contains.

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/somepath/myapp"/>

But this doesn't seem to work, I think it's loaded by http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/startup/SetContextPropertiesRule.html which states it loads everything but the path!

I know I can name the war somepath#myapp.war and it will pick it up but I also deploy to weblogic which isn't happy with a name like this.

Is there some setting I can use to have the path work from the context.xml above?

Thanks David

like image 259
David Kerwick Avatar asked Mar 08 '12 11:03

David Kerwick


People also ask

Where is Tomcat context path?

The context path refers to the location relative to the server's address which represents the name of the web application. By default, Tomcat derives it from the name of the deployed war-file. So if we deploy a file ExampleApp. war, it will be available at http://localhost:8080/ExampleApp.

How do I set context root in web xml?

To Set the Context RootA context root must start with a forward slash (/) and end with a string. In a packaged web module for deployment on the GlassFish Server, the context root is stored in glassfish-web. xml.


2 Answers

The Context path attribute is ignored unless the path is specified in a hard-coded Context in server.xml, which is strongly discouraged, and doesn't take multilevel paths.

The name of the war file, or the name of the Context xml file in tomcat/conf/Catalina/hostname becomes the path of the deployed application.

In your case the latter of the two above is the solution, just make sure you put the .war file outside of the designated appBase for the Host, or you'll deploy the app twice.

In: conf/Catalina/localhost/myapp#path.xml

<?xml version="1.0"?>
<Context docBase="/some/path/to/myapp.war">
</Context>
like image 50
Pidster Avatar answered Oct 19 '22 04:10

Pidster


In /tomcat7/conf/server.xml add below lines inside the element and restart Tomcat to make changes take place.

*change "mycom" to your application name.

<Context path="" docBase="mycom">
  <!-- Default set of monitored resources -->
  <WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
<Context path="ROOT" docBase="ROOT">
  <!-- Default set of monitored resources -->
  <WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>

This will make the default root application appear under context root "/ROOT".

Now your app is accessible at "/" and "/mycom" as well!

like image 37
user3774340 Avatar answered Oct 19 '22 04:10

user3774340