如何使用属性文件在web.xml中设置值? [英] How to set value in web.xml using property file?

查看:89
本文介绍了如何使用属性文件在web.xml中设置值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to know if there is possibility to set an attribute in web.xml by using property file. For example the 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


推荐答案

您无法从属性文件中设置值,但您可以设置属性文件并在运行时读取它。

you can not set value from Properties file but you can set the property file and read its at runtime.

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

这篇关于如何使用属性文件在web.xml中设置值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆