定义 Tomcat servlet 上下文参数 [英] Defining Tomcat servlet context parameters

查看:38
本文介绍了定义 Tomcat servlet 上下文参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:

foo.war 文件包含初始化参数 fooParam=1 的默认值.

foo.war file contains a default value of init parameter fooParam=1.

这是在 foo.war!WEB_INF/web.xml 中定义的,其中包含:

This is defined in foo.war!WEB_INF/web.xml which contains:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app 
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

    <context-param>
      <param-name>fooParam</param-name>
      <param-value>1</param-value>
      <description>
        my parameter "fooParam"
      </description>
    </context-param>

    ...

好的,现在我希望能够在 Tomcat {$CATALINA_HOME}/conf/目录中的配置文件中覆盖它.我在哪里/如何做到这一点???

OK, now I want to be able to override it in a config file in the Tomcat {$CATALINA_HOME}/conf/ directory. Where/how can I do this???

推荐答案

根据上下文 元素:

您可以配置命名值将在网络上可见应用程序作为 servlet 上下文通过嵌套初始化参数 元素在这个里面元素.例如,您可以创建像这样的初始化参数:

Context Parameters

You can configure named values that will be made visible to the web application as servlet context initialization parameters by nesting <Parameter> elements inside this element. For example, you can create an initialization parameter like this:

<Context ...>
  ...
  <Parameter name="companyName" value="My Company, Incorporated"
         override="false"/>
  ...
</Context>

这相当于包含了网络中的以下元素应用程序部署描述符(/WEB-INF/web.xml):

This is equivalent to the inclusion of the following element in the web application deployment descriptor (/WEB-INF/web.xml):

<context-param>
  <param-name>companyName</param-name>
  <param-value>My Company, Incorporated</param-value>
</context-param>

但不需要修改要自定义的部署描述符这个值.

but does not require modification of the deployment descriptor to customize this value.

a 的有效属性 元素如下:

The valid attributes for a <Parameter> element are as follows:

...

关于 override 属性,文档说:

About the override attribute of a <Parameter>, the documentation says:

如果您不希望在 Web 应用程序部署描述符中找到的相同参数名称的 覆盖此处指定的值.默认情况下,允许覆盖.

Set this to false if you do not want a <context-param> for the same parameter name, found in the web application deployment descriptor, to override the value specified here. By default, overrides are allowed.

将其设置为 false 应该可以解决问题.这是如何"部分.

Setting it to false should do the trick. This was the "how" part.

where"部分,阅读参考的介绍上下文容器:

For the "where" part, read refer to the introduction of The Context Container:

对于 Tomcat 6,与 Tomcat 4.x 不同,不建议将 元素直接放置在 server.xml 文件中. 这是因为它使修改上下文配置更具侵入性,因为在不重新启动 Tomcat 的情况下无法重新加载主 conf/server.xml 文件.

For Tomcat 6, unlike Tomcat 4.x, it is NOT recommended to place <Context> elements directly in the server.xml file. This is because it makes modifing the Context configuration more invasive since the main conf/server.xml file cannot be reloaded without restarting Tomcat.

Context 元素可以明确定义:

  • $CATALINA_BASE/conf/context.xml 文件中:Context 元素信息将被所有 web 应用加载.
  • $CATALINA_BASE/conf/[enginename]/[hostname]/context.xml.default 文件中:Context 元素信息将被该主机的所有 web 应用加载.
  • $CATALINA_BASE/conf/[enginename]/[hostname]/ 目录中的单个文件(带有.xml"扩展名)中.文件的名称(减去 .xml 扩展名)将用作上下文路径.可以使用 # 定义多级上下文路径,例如foo#bar.xml 用于 /foo/bar 的上下文路径.可以使用名为 ROOT.xml 的文件定义默认 Web 应用程序.
  • 仅当 $CATALINA_BASE/conf/[enginename]/[hostname]/ 中的应用程序的上下文文件不存在时,在 /META-INF/的单个文件中应用程序文件中的 context.xml.如果 Web 应用程序打包为 WAR,则 /META-INF/context.xml 将被复制到 $CATALINA_BASE/conf/[enginename]/[hostname]/ 和重命名以匹配应用程序的上下文路径.一旦此文件存在,如果将具有更新的 /META-INF/context.xml 的新 WAR 放置在主机的 appBase 中,它将不会被替换.
  • 在主 conf/server.xml 中的 Host 元素内.
  • In the $CATALINA_BASE/conf/context.xml file: the Context element information will be loaded by all webapps.
  • In the $CATALINA_BASE/conf/[enginename]/[hostname]/context.xml.default file: the Context element information will be loaded by all webapps of that host.
  • In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The name of the file (less the .xml extension) will be used as the context path. Multi-level context paths may be defined using #, e.g. foo#bar.xml for a context path of /foo/bar. The default web application may be defined by using a file called ROOT.xml.
  • Only if a context file does not exist for the application in the $CATALINA_BASE/conf/[enginename]/[hostname]/, in an individual file at /META-INF/context.xml inside the application files. If the web application is packaged as a WAR then /META-INF/context.xml will be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to match the application's context path. Once this file exists, it will not be replaced if a new WAR with a newer /META-INF/context.xml is placed in the host's appBase.
  • Inside a Host element in the main conf/server.xml.

除了server.xml,定义Context 元素的文件只能定义一个Context 元素.

With the exception of server.xml, files that define Context elements may only define a single Context element.

这篇关于定义 Tomcat servlet 上下文参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆