scriptfoo
scriptfoo

Reputation: 513

How to make Tomcat obey WAR's context.xml path information

EDIT: This is a duplicate of apache-tomcat-9.0.0.M10: Change Context-Path in META-INF/context.xml not working. In the other question, it is stated that Tomcat ignores context.xml despite the docs and it also seems to be the case here.

I have a WAR copied to Tomcat 9 webapps. This WAR's /META-INF/context.xml is ignored, the resulting path is simply the WAR's name. I do not want it ignored. There is a lot of questions about that file being ignored, with answers "remove the context from Tomcat's conf/server.xml". But there is no such thing in conf/server.xml:

  <Host name="localhost"  appBase="webapps"
        unpackWARs="true" autoDeploy="true">
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="localhost_access_log" suffix=".txt"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />

  </Host>

I have found the following docs:

Then, there is conf/context.xml:

<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
</Context>

but I guess that it is obligatory.

This is the ignored /META-INF/context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/sc">
    <Manager pathname="" />
</Context>

I know that it is read by Tomcat, because introducing any syntax error into it makes Tomcat put the relevant error messages into its logs. Yet it is ignored anyways. I have found the following docs:

This "/META-INF/context.xml" file will be automatically copied to "$CATALINA_HOME/conf/[enginename]/[hostname]/", after being renamed to mirror the application's context path.

Once the "context.xml" file has been renamed and copied to the "[hostname]" directory, it will not be replaced, even if the WAR is updated with a new "/META-INF/context.xml" file.

which is confusing: what is the idea behind reading the WAR's context.xml just one time and then ignoring it? By the way, the directory conf/Catalina/localhost/ is empty anyways.

I tried to increase the log level to FINE, but I cannot make the logger report, how the context files are handled.

My question: if I have no context defined in conf/server.xml, how to make Tomcat not ignore /META-INF/context.xml in my WAR, including the cases where the context's contents change?

Upvotes: 0

Views: 2638

Answers (1)

Christopher Schultz
Christopher Schultz

Reputation: 20862

You have asked several questions, here.

[Why is my path being ignored?]

(Technically you didn't ask this, but you clearly want to know why)

If you read the documentation, you'll see that Tomcat explicitly ignores the <Context> element's path attribute when it's bundled in a WAR file. There are a bunch of reasons for this including clarity and security.

What is the idea behind reading the WAR's context.xml just one time and then ignoring it?

Tomcat reads your application's META-INF/context.xml file on deployment. If you want it re-read, then redeploy your application (don't just just update/restart it). The file is read once and not overwritten to allow system administrators to override the contents if they disagree with developers/WAR-packagers over what should be configured.

How [do I] make Tomcat not ignore /META-INF/context.xml in my WAR, including the cases where the context's contents change?

You must redeploy the application. This may require an undeploy/deploy operation.

Upvotes: 1

Related Questions