Maik
Maik

Reputation: 1702

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

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.

Upvotes: 4

Views: 13203

Answers (2)

wmorrison365
wmorrison365

Reputation: 6318

To spare anyone else the time and effort of trying to deploy to a different path using META-INF/context.xml, and with respect to Pavel's answer and others, it would seem that it is simply not possible (in tomcat 9.x).

I was hoping to deploy to the root context through configuration of the war's META-INF/context.xml, but it would seem that it's not currently possible (tomcat 9.x).

With Host copyXML="true", the context.xml is deployed (to tomcat's conf/Catalina/<host>) using the war's base name. Due to path configuration rules, this then takes precendence in determining the path. That is, the path is named according to the .xml's base name - hence the war's basename. As the docs and the answer mentioned address, the Context path field is not respected unless it is used in the server.xml.

This is actually in the docs but is not immdiately obvious and you have to piece it together a bit, especially as the path attribute is readily available to the META-INF/context.xml.

See: Tomcat 9.0/ Config Guide/ Naming

When autoDeploy or deployOnStartup operations are performed by a Host, the name and context path of the web application are derived from the name(s) of the file(s) that define(s) the web application. Consequently, the context path may not be defined in a META-INF/context.xml embedded in the application and there is a close relationship between the context name, context path, context version and the base file name (the name minus any .war or .xml extension) of the file.

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.

Tomcat 9.0/ Config Guide/ Defining a Context

  • Individual Context elements may be explicitly defined:
    • In an individual file at /META-INF/context.xml inside the application files. Optionally (based on the Host's copyXML attribute) this may be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to application's base file name plus a ".xml" extension.

Tomcat 9.0/ Config Guide/ Defining a Context/ Common Attributes/ path attribute

This attribute must only be used when statically defining a Context in server.xml. In all other circumstances, the path will be inferred from the filenames used for either the .xml context file or the docBase.

It seems to me, after a lot of play, that the only use of the META-INF/context.xml is in defining other configuration items of an application on deployment - e.g. resource configuration - though I'd be interested to know other's thoughts.

See Pavel's answer and others (below) for how to deploy to a different context path and to the root context (without using META-INF/context.xml - no autodeploy here(!)):

Upvotes: 2

Pavel Lechev
Pavel Lechev

Reputation: 953

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.

Upvotes: 6

Related Questions