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

Java 如何使用属性文件在web.xml中设置值?,java,servlets,properties,web.xml,Java,Servlets,Properties,Web.xml,我想知道是否可以使用属性文件在web.xml中设置属性。例如web.xml: <context-param> <param-name>Map.MyJNDI</param-name> <param-value>java:comp/env/jdbc/${my.computer}</param-value> </context-param> 不能从属性文件中设置值,但可以设置属性文件并在运行时读取其值 <conte

我想知道是否可以使用属性文件在
web.xml
中设置属性。例如web.xml:

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

不能从
属性
文件中设置值,但可以设置属性文件并在运行时读取其值

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

希望这对其他人也有帮助

在web.xml中不能这样替换值

如果可能的话,我可以建议一个选项,就是为每个环境创建一个带有占位符的模板web.xml,并且在构建过程中为每个环境创建一个步骤,该步骤将替换该环境所需属性文件中的所需值。

检查这些:--可能重复的
<context-param>
    <param-name>propfile</param-name>
    <param-value>myproject.properties</param-value>
</context-param>
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());