Java 理解 tomcat 6 中的 context.xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1661052/
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-08-12 18:49:24  来源:igfitidea点击:

Understanding context.xml in tomcat 6

javatomcat6context.xml

提问by Carsten

I created a mainly empty dynamic web project in eclipse.

我在 eclipse 中创建了一个主要是空的动态 web 项目。

It has

它有

  • no servlets
  • no jsp files
  • 没有 servlet
  • 没有jsp文件

The web.xml is

web.xml 是

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>testprojekt</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

and I added a context.xmlto its META-INFfolder

context.xml在它的META-INF文件夹中添加了一个

<?xml version="1.0" encoding="UTF-8"?>
<Context>
        <Parameter name="companyName" value="My Company, Incorporated"  override="false"/>
</Context>

I exported this project as a WAR file. With the following structure:

我将此项目导出为 WAR 文件。具有以下结构:

user@system:$ tree
.
|-- META-INF
|   |-- MANIFEST.MF
|   `-- context.xml
`-- WEB-INF
    |-- classes
    |-- lib
    `-- web.xml

4 directories, 3 files

When I deploy the project to a local tomcat (Apache Tomcat/6.0.20) Everything works as expected. Meaning, the context.xml is copied to /conf/Catalina/localhost and renamed to testprojekt.xml.

当我将项目部署到本地 tomcat (Apache Tomcat/6.0.20) 时,一切都按预期工作。意思是,context.xml 被复制到 /conf/Catalina/localhost 并重命名为 testprojekt.xml。

When I edit the testprojekt.xml to:

当我将 testprojekt.xml 编辑为:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
  <Parameter name="companyName" value="My BLAH Company, Incorporated"  override="false"/>
</Context>

I see the following output in the catalina.out:

我在 catalina.out 中看到以下输出:

02.11.2009 13:21:35 org.apache.catalina.startup.HostConfig checkResources
INFO: Undeploying context [/testprojekt]
02.11.2009 13:21:35 org.apache.catalina.core.StandardContext resourcesStart
SCHWERWIEGEND: Error starting static Resources
java.lang.IllegalArgumentException: Document base /opt/tomcat6/webapps/testprojekt does not exist or is not a readable directory
        at org.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.java:142)
        at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4048)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4217)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:526)
        at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:630)
        at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:556)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:491)
        at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1274)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:296)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
        at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1337)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1601)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1610)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1590)
        at java.lang.Thread.run(Thread.java:619)
02.11.2009 13:21:35 org.apache.catalina.core.StandardContext start
SCHWERWIEGEND: Error in resourceStart()
02.11.2009 13:21:35 org.apache.catalina.core.StandardContext start
SCHWERWIEGEND: Error getConfigured
02.11.2009 13:21:35 org.apache.catalina.core.StandardContext start
SCHWERWIEGEND: Context [/testprojekt] startup failed due to previous errors
02.11.2009 13:21:35 org.apache.catalina.core.StandardContext stop
INFO: Container org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/testprojekt] has not been started

Why is that? Is that the expected result? What is the right way to change parameters in a context.xml?

这是为什么?这是预期的结果吗?在 a 中更改参数的正确方法是context.xml什么?

Thank you in advance. Regards,

先感谢您。问候,

采纳答案by ZZ Coder

I think this is a bug in Tomcat. I filed a bug report but they claim it works as designed. Tomcat has 3 modes of deployment: Directory, WAR and Context Fragment. In your case, it gets confused when reloading.

我认为这是Tomcat中的一个错误。我提交了一个错误报告,但他们声称它按设计工作。Tomcat 有 3 种部署模式:Directory、WAR 和 Context Fragment。在您的情况下,重新加载时会感到困惑。

Here is the sequence leading to the error,

这是导致错误的顺序,

  1. When you deploy the WAR, the context fragment (META-INF/context.xml) is copied to conf/Catalina/[host] directory.
  2. When you modifies the fragment, it correctly detects the change so redeployment is triggered.
  3. However, it forgets this is a WAR deployment and treats it as Directory deployment. The directory is removed by undelpoy so you get the error.
  1. 部署 WAR 时,上下文片段 (META-INF/context.xml) 将复制到 conf/Catalina/[host] 目录。
  2. 当您修改片段时,它会正确检测更改,因此会触发重新部署。
  3. 但是,它忘记了这是一个 WAR 部署,并将其视为目录部署。该目录已被 undelpoy 删除,因此您会收到错误消息。

If you only change the XML in META-INF, everything should work for you.

如果您只更改 META-INF 中的 XML,一切都应该适合您。

回答by ChadNC

Your question is a tad ambiguous but I'll give it a try.

你的问题有点模棱两可,但我会试一试。

Delete the textprojekt.xml that you edited, make those same changes in the context.xml for your project, and restart tomcat.

删除您编辑的 textprojekt.xml,在您的项目的 context.xml 中进行相同的更改,然后重新启动 tomcat。

It may be caused by a conflict between the two xml files. Changing the xml file that is created by tomcat does nto change the context.xml file for the webapp. At least it doesn't change on tomcat6.0.18 that I use for dev work.

可能是两个xml文件冲突造成的。更改由 tomcat 创建的 xml 文件不会更改 webapp 的 context.xml 文件。至少它在我用于开发工作的 tomcat6.0.18 上没有改变。

Hope that's helpful.

希望这有帮助。