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.
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:
- Disable autoDeploy and deployOnStartup and define all Contexts in server.xml
- 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With