java - 如何使用属性文件在 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>

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/

相关文章:

java - 如何从 JSP 中的查询字符串 URL 中删除 %20

java - 所有Android设备的通用存储路径

c# - 如何编写属性以便访问内部变量?

java - 无法将通用列表添加到通用 map

java - 调用对应的 set 方法时获取对象值

java - 如何访问servlet并下载附件?

java - 在 Servlet 上调用 DAO 方法

java - jsp/servlet 中的 Web 应用程序 - 使用 fmt 进行本地化 :message

c# - 静态属性的总和不正确

c# - 是否可以在其get和set中获取属性名称的字符串?