如何在启动时在 Tomcat 配置中指定系统属性?

发布于 2024-07-11 04:15:14 字数 254 浏览 3 评论 0原文

据我所知,我可以通过使用 -D 参数传递参数来指定 Tomcat 的系统属性,例如“-Dmy.prop=value”。

我想知道是否有一种更简洁的方法可以通过在 context.xml 文件或其他 tomcat 配置文件中指定属性值来实现此目的。 我想这样做,因为首先,更容易跟踪我的属性,其次,我正在运行多个上下文,并且我不知道如何通过 -D 参数指定特定于上下文的属性。

我使用的是 Tomcat 5.5 版本。

I understand that I can specify system properties to Tomcat by passing arguments with the -D parameter, for example "-Dmy.prop=value".

I am wondering if there is a cleaner way of doing this by specifying the property values in the context.xml file or some other tomcat configuration file. I would like to do this because, first, it is easier to keep track of my properties, and second, I have multiple contexts running and I don't know how I would specify context-specific properties through the -D parameter.

I am using Tomcat version 5.5.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

本宫微胖 2024-07-18 04:15:14

cliff.meyers 的原始答案建议使用 仅使用 System.getProperty()

根据 Tomcat 6.0 文档 用于 JNDI< /a>. 这意味着它不会对 System.getProperty() 产生任何影响。

对于 cliff.meyers 示例中的 ,以下代码

System.getProperty("SMTP_PASSWORD");

将返回 null,而不是值“abc123ftw”。

根据 Tomcat 6 文档,要使用 ,您必须编写如下代码才能使用

// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");

// Look up our data source
String s = (String)envCtx.lookup("SMTP_PASSWORD");

警告:I还没有实际尝试过上面的例子。 但我已经尝试使用 System.getProperty() 来,但这绝对行不通。

cliff.meyers's original answer that suggested using <env-entry> will not help when using only System.getProperty()

According to the Tomcat 6.0 docs <env-entry> is for JNDI. So that means it won't have any effect on System.getProperty().

With the <env-entry> from cliff.meyers's example, the following code

System.getProperty("SMTP_PASSWORD");

will return null, not the value "abc123ftw".

According to the Tomcat 6 docs, to use <env-entry> you'd have to write code like this to use <env-entry>:

// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");

// Look up our data source
String s = (String)envCtx.lookup("SMTP_PASSWORD");

Caveat: I have not actually tried the example above. But I have tried <env-entry> with System.getProperty(), and that definitely does not work.

药祭#氼 2024-07-18 04:15:14

(更新:如果我可以删除这个答案,我会的,尽管它已被接受,但我不能。我正在更新描述以提供更好的指导,并阻止人们使用我在原始答案中概述的不良做法)。

您可以通过上下文或环境参数(例如在 context.xml 中)指定这些参数。 请参阅本页上标题为“上下文参数”和“环境条目”的部分:

http://tomcat.apache.org/tomcat-5.5-doc/config/context.html

正如@netjeff指出的,这些值将通过 Context.lookup(String) 方法获得,而不是作为系统参数。

指定这些值的另一种方法是在您正在部署的 Web 应用程序的 web.xml 文件中定义变量(见下文)。 正如 @Roberto Lo Giacco 指出的那样,这通常被认为是一种糟糕的做法,因为部署的工件不应该是特定于环境的。 但是,如果您确实想这样做,下面是配置片段:

<env-entry>
    <env-entry-name>SMTP_PASSWORD</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>abc123ftw</env-entry-value>
</env-entry>

(Update: If I could delete this answer I would, although since it's accepted, I can't. I'm updating the description to provide better guidance and discourage folks from using the poor practice I outlined in the original answer).

You can specify these parameters via context or environment parameters, such as in context.xml. See the sections titled "Context Parameters" and "Environment Entries" on this page:

http://tomcat.apache.org/tomcat-5.5-doc/config/context.html

As @netjeff points out, these values will be available via the Context.lookup(String) method and not as System parameters.

Another way to do specify these values is to define variables inside of the web.xml file of the web application you're deploying (see below). As @Roberto Lo Giacco points out, this is generally considered a poor practice since a deployed artifact should not be environment specific. However, below is the configuration snippet if you really want to do this:

<env-entry>
    <env-entry-name>SMTP_PASSWORD</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>abc123ftw</env-entry-value>
</env-entry>
萌无敌 2024-07-18 04:15:14

一般来说,您不应该依赖系统属性来配置 Web 应用程序 - 它们可能用于配置容器(例如 Tomcat),但不能用于配置在 tomcat 内运行的应用程序。

Cliff.meyers 已经提到了您应该在 Web 应用程序中使用的方式。 这是标准方式,也适合您通过 context.xml 或 server.xml 方式进行配置的问题。

也就是说,如果您确实需要 tomcat 中的系统属性或其他 jvm 选项(例如最大内存设置),则应该创建一个名为“bin/setenv.sh”或“bin/setenv.bat”的文件。 这些文件不存在于您下载的标准存档中,但如果存在,则内容会在启动期间执行(如果您通过startup.sh/startup.bat启动tomcat)。 这是将您自己的设置与标准 tomcat 设置分开的好方法,并使更新变得更加容易。 无需调整startup.sh 或catalina.sh。

(如果您将 tomcat 作为 Windows servive 执行,则通常使用 tomcat5w.exe、tomcat6w.exe 等来配置服务的注册表设置。)

编辑:此外,另一种可能性是选择 JNDI 资源

Generally you shouldn't rely on system properties to configure a webapp - they may be used to configure the container (e.g. Tomcat) but not an application running inside tomcat.

cliff.meyers has already mentioned the way you should rather use for your webapplication. That's the standard way, that also fits your question of being configurable through context.xml or server.xml means.

That said, should you really need system properties or other jvm options (like max memory settings) in tomcat, you should create a file named "bin/setenv.sh" or "bin/setenv.bat". These files do not exist in the standard archive that you download, but if they are present, the content is executed during startup (if you start tomcat via startup.sh/startup.bat). This is a nice way to separate your own settings from the standard tomcat settings and makes updates so much easier. No need to tweak startup.sh or catalina.sh.

(If you execute tomcat as windows servive, you usually use tomcat5w.exe, tomcat6w.exe etc. to configure the registry settings for the service.)

EDIT: Also, another possibility is to go for JNDI Resources.

一个人的旅程 2024-07-18 04:15:14

也可以让 ServletContextListener 设置系统属性:

import java.util.Enumeration;
import javax.servlet.*;

public class SystemPropertiesHelper implements
        javax.servlet.ServletContextListener {
    private ServletContext context = null;

    public void contextInitialized(ServletContextEvent event) {
        context = event.getServletContext();
        Enumeration<String> params = context.getInitParameterNames();

        while (params.hasMoreElements()) {
          String param = (String) params.nextElement();
          String value = 
            context.getInitParameter(param);
          if (param.startsWith("customPrefix.")) {
              System.setProperty(param, value);
          }
        }
    }

    public void contextDestroyed(ServletContextEvent event) {
    }
}

然后将其放入 web.xml 中(context.xml 也应该可以)

<context-param>
        <param-name>customPrefix.property</param-name>
        <param-value>value</param-value>
        <param-type>java.lang.String</param-type>
</context-param>

<listener>
    <listener-class>servletUtils.SystemPropertiesHelper</listener-class>    
</listener>

它对我有用。

It's also possible letting a ServletContextListener set the System properties:

import java.util.Enumeration;
import javax.servlet.*;

public class SystemPropertiesHelper implements
        javax.servlet.ServletContextListener {
    private ServletContext context = null;

    public void contextInitialized(ServletContextEvent event) {
        context = event.getServletContext();
        Enumeration<String> params = context.getInitParameterNames();

        while (params.hasMoreElements()) {
          String param = (String) params.nextElement();
          String value = 
            context.getInitParameter(param);
          if (param.startsWith("customPrefix.")) {
              System.setProperty(param, value);
          }
        }
    }

    public void contextDestroyed(ServletContextEvent event) {
    }
}

And then put this into your web.xml (should be possible for context.xml too)

<context-param>
        <param-name>customPrefix.property</param-name>
        <param-value>value</param-value>
        <param-type>java.lang.String</param-type>
</context-param>

<listener>
    <listener-class>servletUtils.SystemPropertiesHelper</listener-class>    
</listener>

It worked for me.

悲念泪 2024-07-18 04:15:14

在 tomcat 配置上设置系统属性的另一种方法是使用 CATALINA_OPTS 环境变量

An alternative to setting the system property on tomcat configuration is to use CATALINA_OPTS environment variable

煞人兵器 2024-07-18 04:15:14

Apache wiki 中解决了这个问题。

问题:“我可以为每个 Web 应用程序设置不同的 Java 系统属性吗?”

答案:不需要。如果您可以编辑Tomcat的启动脚本(或者更好地创建一个setenv.sh文件),您可以向Java添加“-D”选项。 但在 Java 中,同一 JVM 中的不同类无法具有不同的系统属性值。 还有一些其他方法可用,例如使用 ServletContext.getContextPath() 获取 Web 应用程序的上下文名称并相应地定位一些资源,或者在 Web 应用程序的 WEB-INF/web.xml 文件中定义元素,然后设置Tomcat 上下文文件 (META-INF/context.xml) 中的值。 请参阅 http://tomcat.apache.org/tomcat-7.0-doc/ config/context.html

http://wiki.apache.org/tomcat/HowTo#Can_I_set_Java_system_properties_ Differently_for_each_webapp.3F

This question is addressed in the Apache wiki.

Question: "Can I set Java system properties differently for each webapp?"

Answer: No. If you can edit Tomcat's startup scripts (or better create a setenv.sh file), you can add "-D" options to Java. But there is no way in Java to have different values of system properties for different classes in the same JVM. There are some other methods available, like using ServletContext.getContextPath() to get the context name of your web application and locate some resources accordingly, or to define elements in WEB-INF/web.xml file of your web application and then set the values for them in Tomcat context file (META-INF/context.xml). See http://tomcat.apache.org/tomcat-7.0-doc/config/context.html .

http://wiki.apache.org/tomcat/HowTo#Can_I_set_Java_system_properties_differently_for_each_webapp.3F

女皇必胜 2024-07-18 04:15:14

您可以将必要的属性添加到/conf目录中的catalina.properties文件中。

参考: https://tomcat.apache.org/tomcat-8.0- doc/config/index.html

所有系统属性均可用,包括使用 -D 设置的属性
语法,由 JVM 自动提供的语法以及
在 $CATALINA_BASE/conf/catalina.properties 文件中配置。

You could add necessary properties to catalina.properties file in <tomcat installation directory>/conf directory.

Reference: https://tomcat.apache.org/tomcat-8.0-doc/config/index.html

All system properties are available including those set using the -D
syntax, those automatically made available by the JVM and those
configured in the $CATALINA_BASE/conf/catalina.properties file.

扮仙女 2024-07-18 04:15:14

如果您想根据文档在上下文中定义环境变量,您应该如下定义它们

<Context ...>
  ...
  <Environment name="maxExemptions" value="10"
         type="java.lang.Integer" override="false"/>
  ...
</Context>

也可以按如下方式使用它们:

((Context)new InitialContext().lookup("java:comp/env")).lookup("maxExemptions")

您应该得到 10 作为输出。

If you want to define an environment variable in your context base on documentation you shod define them as below

<Context ...>
  ...
  <Environment name="maxExemptions" value="10"
         type="java.lang.Integer" override="false"/>
  ...
</Context>

Also use them as below:

((Context)new InitialContext().lookup("java:comp/env")).lookup("maxExemptions")

You should get 10 as output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文