Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apache-tomcat-9.0.0.M10: Change Context-Path in META-INF/context.xml not working

Tags:

java

xml

tomcat

I have a standard Tomcat9 installation. I just put a helloworld.war in the webapps folder and inside the META-INF I put the context.xml with a different path like: <Context path="/newcontext" />

But the context is still http://localhost/helloworld instead http://localhost/newcontext.

I tried so many things, but it is not possible to get Tomcat convinced to use the new path. Anybody who can help? Thanks.

like image 577
Maik Avatar asked Sep 07 '25 09:09

Maik


1 Answers

I found that Tomcat will simply ignore the path attribute defined in context.xml. This is not explicitly stated in their documentation (https://tomcat.apache.org/tomcat-9.0-doc/config/context.html) but it is a behaviour I keep observing in every application we build.

As per Tomcat docs:

In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The context path and version will be derived from the base name of the file (the file name less the .xml extension). This file will always take precedence over any context.xml file packaged in the web application's META-INF directory.

Though this discusses the conf files placed outside your WAR, it appears that the same applies to META-INF/context.xml

This is what the documentation suggests for your pains:

If you want to deploy a WAR file or a directory using a context path that is not related to the base file name then one of the following options must be used to prevent double-deployment:

  1. Disable autoDeploy and deployOnStartup and define all Contexts in server.xml
  2. Locate the WAR and/or directory outside of the Host's appBase and use a context.xml file with a docBase attribute to define it.

yet, then they go on to discourage Option 1.

I personally found the easiest solution to be simply to name your WAR file appropriately for non-root contexts and for root contexts (/) to simply use ROOT.war as per Tomcat's conventions.

like image 84
Pavel Lechev Avatar answered Sep 10 '25 10:09

Pavel Lechev