java Tomcat 7 上下文参数覆盖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14696601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:04:58  来源:igfitidea点击:

Tomcat 7 context parameter override

javatomcatservletsconfiguration

提问by Jeff Storey

I am trying to override a parameter in my application's web.xml file by creating a context.xml file in <tomcatHome>/conf/Catalina/localhost

我试图通过在我的应用程序的 web.xml 文件中创建一个 context.xml 文件来覆盖参数 <tomcatHome>/conf/Catalina/localhost

The context.xml file looks like

context.xml 文件看起来像

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/myapp">    
    <Parameter name="port" value="100" override="1"/>
</Context>

but I get an error saying

但我收到一条错误消息

java.lang.IllegalArgumentException: Document base <path-to-tomcat> apache-tomcat-7.0.35/webapps/context does not exist or is not a readable directory

If I put the <Parameter name="port" value="100" override="1"/>directly in the context.xmlin <tomcat-home>/context.xmlthen it works.

如果我<Parameter name="port" value="100" override="1"/>直接把它放在里面context.xml<tomcat-home>/context.xml那么它就起作用了。

Can someon explain what I am doing incorrectly?

有人可以解释我做错了什么吗?

回答by informatik01

It is because there is no such application context with the name context. In other words there is no web app with the name contextdeployed to the webappsdirectory.

这是因为没有名称为context 的应用程序上下文。换句话说,没有将名称上下文部署到webapps目录的Web 应用程序。

Form the official Tomcat 7 documentation related to Defining a context:

形成与定义上下文相关的官方 Tomcat 7 文档:

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.

  • 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.

  • Inside a Host element in the main conf/server.xml.

可以明确定义单个 Context 元素:

  • 在应用程序文件内 /META-INF/context.xml 的单个文件中。可以选择(基于主机的 copyXML 属性)将其复制到 $CATALINA_BASE/conf/[enginename]/[hostname]/ 并重命名为应用程序的基本文件名加上“.xml”扩展名。

  • 在 $CATALINA_BASE/conf/[enginename]/[hostname]/ 目录中的单个文件(带有“.xml”扩展名)中。上下文路径和版本将从文件的基本名称(文件名减去 .xml 扩展名)派生。此文件将始终优先于打包在 Web 应用程序的 META-INF 目录中的任何 context.xml 文件。

  • 在主 conf/server.xml 中的 Host 元素内。

So to make it work, name your custom file not context.xml, but your_app_name.xml.
In your case it will be (if I understood you correctly) myapp.xml.

因此,要使其工作,请不要将您的自定义文件命名为context.xml,而是your_app_name.xml
在您的情况下,它将是(如果我理解正确的话)myapp.xml

This should work. I have just tested it.

这应该有效。我刚刚测试过。

myapp.xml

我的应用程序.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>    
    <Parameter name="port" value="100" override="1"/>
</Context>


P.S.

聚苯乙烯

And you can get without pathattribute, so don't include it.
From the Apache Tomcat 7 documentation:

并且您可以在没有path属性的情况下获取,所以不要包含它。
来自Apache Tomcat 7 文档

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.

Even when statically defining a Context in server.xml, this attribute must not be set unless either the docBase is not located under the Host's appBase or both deployOnStartup and autoDeploy are false. If this rule is not followed, double deployment is likely to result.

只有在 server.xml 中静态定义 Context 时才必须使用此属性。在所有其他情况下,将从用于 .xml 上下文文件或 docBase 的文件名推断路径。

即使在 server.xml 中静态定义上下文,也不得设置此属性,除非 docBase 不在主机的 appBase 下,或者 deployOnStartup 和 autoDeploy 都为 false。如果不遵守此规则,可能会导致双重部署。