我想知道是否可以使用属性文件在 web.xml
中设置属性。例如 web.xml:
<context-param>
<param-name>Map.MyJNDI</param-name>
<param-value>java:comp/env/jdbc/${my.computer}</param-value>
</context-param>
和application.properties
将是:
# My computer's name
my.computer=eniac
最佳答案
您不能从 Properties
文件中设置值,但您可以设置属性文件并在运行时读取它。
<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());
希望这对其他人也有帮助。
关于java - 如何使用属性文件在 web.xml 中设置值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3250219/