简体   繁体   English

如何使用属性文件在web.xml中设置值?

[英]How to set value in web.xml using property file?

I would like to know if there is possibility to set an attribute in web.xml by using property file. 我想知道是否有可能通过使用属性文件在web.xml设置属性。 For example the web.xml: 例如web.xml:

<context-param>
  <param-name>Map.MyJNDI</param-name>
  <param-value>java:comp/env/jdbc/${my.computer}</param-value>
</context-param>

and application.properties would be: application.properties将是:

# My computer's name
my.computer=eniac

you can not set value from Properties file but you can set the property file and read its at runtime. 您无法从Properties文件中设置值,但您可以设置属性文件并在运行时读取它。

<context-param>
    <param-name>propfile</param-name>
    <param-value>myproject.properties</param-value>
</context-param>

then read the property file at runtime. 然后在运行时读取属性文件。

MyServlet myServlet = new MyServlet();

Properties  properties = new Properties();
//  get the properties file name 
String propfile = myServlet.getInitParameter("propfile");

// load the file 
properties.load(getClass().getClassLoader().getResourceAsStream(propfile));

// get the desire properties 
Object propName = properties.get("my.computer");
// out.println(propName.toString());

hope this help other too. 希望对其他人有所帮助。

You can't have values substituted in web.xml like that. 您不能在web.xml中替换值。

One option I can suggest if possible just have a template web.xml with place holder for values and during build for each environment have a step in build process that will substitute required values from required properties file of that environment. 我可以建议的一个选项,如果可能的话,只需要一个带有值的占位符的模板web.xml,并且在构建期间为每个环境创建一个步骤,构建过程将替换该环境的必需属性文件中的必需值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM