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

编程入门 行业动态 更新时间:2024-10-14 04:23:16
本文介绍了如何在启动时在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> <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:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630246.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:启动时   属性   如何在   系统   Tomcat

发布评论

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

>www.elefans.com

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