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

编程入门 行业动态 更新时间:2024-10-14 06:25:06
本文介绍了如何在启动时在 Tomcat 配置中指定系统属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道我可以通过使用 -D 参数传递参数来为 Tomcat 指定系统属性,例如-Dmy.prop=value".

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

我使用的是 Tomcat 5.5 版.

解决方案

(更新:如果我可以删除这个答案,我会的,虽然因为它被接受了,我不能.我正在更新描述以提供更好的指导和劝阻人们不要使用我在原始答案中概述的糟糕做法).

您可以通过上下文或环境参数指定这些参数,例如在 context.xml 中.请参阅此页面上标题为上下文参数"和环境条目"的部分:

tomcat.apache/tomcat-5.5-doc/config/context.html

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

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

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

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.

解决方案

(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:

tomcat.apache/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>

更多推荐

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

本文发布于:2023-11-25 15:11:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630247.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:启动时   属性   如何在   系统   Tomcat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!