如何在 tomcat6 中向 conf/context.xml 添加多个 Context 元素

如何在 tomcat6 中向 conf/context.xml 添加多个 Context 元素

我现有的 conf/context.xml 文件已经有一个元素;WEB-INF/web.xml

我想添加另一个以允许本地主机访问我的监控中的 /manager/status 位置,但是添加一个新的 Context 元素,如下所示;

conf/context.xml

----
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
<Context path="/manager" privileged="true"
         docBase="/usr/share/tomcat6/webapps/manager">
         <Valve className="org.apache.catalina.valves.RemoteAddrValve"
                allow="127\.0\.0\.1"/>
</Context>
----

破坏了 xml,因为我推测文件需要一个根元素来进行 lint/parse,ok 严重:解析第 6 行第 2 列的致命错误:文档中根元素后面的标记必须格式正确。

org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)

答案1

context.xml 文件中的详细信息适用于所有上下文。如果您想要为某个特定上下文指定具体详细信息,请在 $CATALINA_BASE/conf/[enginename]/[hostname]/ 中创建一个带有上下文标签的文件

例如,conf/catalina/localhost/manager.xml 包含:

<Context privileged="true"
         docBase="/usr/share/tomcat6/webapps/manager">
         <Valve className="org.apache.catalina.valves.RemoteAddrValve"
                allow="127\.0\.0\.1"/>
</Context>

请注意,我们不需要路径,因为文件名为 manager.xml

阅读此内容了解更多详细信息:http://tomcat.apache.org/tomcat-6.0-doc/config/context.html

答案2

对于 Tomcat 5,与 Tomcat 4.x 不同,不建议将元素直接放在 server.xml 文件中。这是因为如果不重新启动 Tomcat,主 conf/server.xml 文件无法重新加载,这会使修改 Context 配置更具侵入性。

上下文元素可以明确定义:

在 $CATALINA_HOME/conf/context.xml 文件中:所有 webapps 都将加载 Context 元素信息。在 $CATALINA_HOME/conf/[enginename]/[hostname]/context.xml.default 文件中:该主机的所有 webapps 都将加载 Context 元素信息。在 $CATALINA_HOME/conf/[enginename]/[hostname]/ 目录中的单个文件(带有“.xml”扩展名)中。文件的名称(不含 .xml)扩展名将用作上下文路径。可以使用 # 定义多级上下文路径,例如,对于上下文路径 /foo/bar,可以使用 foo#bar.xml。可以使用名为 ROOT.xml 的文件定义默认 web 应用程序。仅当 $CATALINA_HOME/conf/[enginename]/[hostname]/ 中不存在应用程序的上下文文件时;在应用程序文件内的 /META-INF/context.xml 中的单个文件中。如果 Web 应用程序打包为 WAR,则 /META-INF/context.xml 将被复制到 $CATALINA_HOME/conf/[enginename]/[hostname]/ 并重命名以匹配应用程序的上下文路径。一旦此文件存在,如果将具有较新 /META-INF/context.xml 的新 WAR 放置在主机的 appBase 中,则不会替换它。在主 conf/server.xml 中的 Host 元素内

除 server.xml 外,定义 Context 元素的文件只能定义单个 Context 元素。

相关内容