如何使Tomcat遵守WAR文件的context.xml路径信息

huangapple go评论102阅读模式
英文:

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

问题

这是一个重复的问题,原文链接在https://stackoverflow.com/questions/39589451/apache-tomcat-9-0-0-m10-change-context-path-in-meta-inf-context-xml-not-working中。在其他问题中,提到尽管文档中提到Tomcat会忽略context.xml,但实际上并不是这样,在这里也是如此。

我有一个复制到Tomcat 9的WAR文件夹中的WAR包。该WAR包的/META-INF/context.xml被忽略,最终的路径只是WAR包的名称。我不希望它被忽略。关于忽略该文件的问题有很多,答案是“从Tomcat的conf/server.xml中删除上下文”。但是在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>

我找到了以下文档:

然后,还有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>

但我猜这是强制性的。

这是被忽略的/META-INF/context.xml

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

我知道Tomcat会读取它,因为在其中引入任何语法错误会导致Tomcat将相关错误消息放入其日志中。然而,它仍然被忽略。我找到了以下文档:

这个"/META-INF/context.xml"文件将被自动复制到"$CATALINA_HOME/conf/[enginename]/[hostname]/",并在复制后重命名为应用程序的上下文路径。

一旦"context.xml"文件被重命名并复制到"[hostname]"目录中,即使WAR包中有新的"/META-INF/context.xml"文件,它也不会被替换。

这很令人困惑:只读取一次WAR包的context.xml文件然后忽略它的背后思想是什么?顺便说一下,目录conf/Catalina/localhost/无论如何都是空的。

我尝试将日志级别提高到FINE,但无法让记录器报告上下文文件是如何处理的。

我的问题:如果在conf/server.xml中没有定义上下文,如何使Tomcat不忽略我的WAR包中的/META-INF/context.xml,包括上下文内容发生更改的情况?

英文:

EDIT: This is a duplicate of https://stackoverflow.com/questions/39589451/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:

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

  &lt;/Host&gt;

I have found the following docs:

Then, there is conf/context.xml:

&lt;Context&gt;
    &lt;WatchedResource&gt;WEB-INF/web.xml&lt;/WatchedResource&gt;
    &lt;WatchedResource&gt;WEB-INF/tomcat-web.xml&lt;/WatchedResource&gt;
    &lt;WatchedResource&gt;${catalina.base}/conf/web.xml&lt;/WatchedResource&gt;
&lt;/Context&gt;

but I guess that it is obligatory.

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;Context path=&quot;/sc&quot;&gt;
    &lt;Manager pathname=&quot;&quot; /&gt;
&lt;/Context&gt;

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?

答案1

得分: 1

你提出了一些问题,这里是翻译好的部分:

> [为什么我的 path 被忽略了?]
>
> (严格来说,你没有直接问这个问题,但你显然想知道为什么)

如果你阅读了文档,你会看到Tomcat在WAR文件中显式忽略了 <Context> 元素的 path 属性。这其中有很多原因,包括清晰度和安全性。

> 读取WAR的 context.xml 一次然后忽略它的背后思想是什么?

Tomcat在部署时会读取你的应用程序的 META-INF/context.xml 文件。如果你想要重新读取它,请重新部署你的应用程序(不要仅仅更新/重启它)。该文件只会被读取一次,不会被覆盖,以允许系统管理员覆盖其中的内容,如果他们与开发人员/WAR打包者对于配置应该如何进行有不同意见。

> 如何使Tomcat不忽略我的WAR中的 /META-INF/context.xml,包括上下文内容发生更改的情况?

你必须重新部署应用程序。这可能需要进行取消部署/部署操作。

英文:

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 &lt;Context&gt; 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.

huangapple
  • 本文由 发表于 2020年7月31日 21:06:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63192436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定