【发布时间】:2011-10-04 13:13:08
【问题描述】:
我正在尝试在我的 java Web 应用程序中读取属性文件。我已经尝试过这些解决方案:
Where to place and how to read configuration resource files in servlet based application?
Howto access properties file from Java EE web application?
但没有一个对我有用。
这是我的应用程序的结构:
读取属性文件的代码放在A类中,即使我放了绝对路径也不起作用。 A 是一个普通的 Java 类。 但是,如果将读取属性代码放在 servlet 类 (ProcessRequest.java) 中,那么一切都会像魅力一样发挥作用
这是我使用的代码:
public class A {
public A() {
try {
Properties p = new Properties();
p.load(this.getClass().getClassLoader().getResourceAsStream("/a.properties"));
String n = p.getProperty("name");
System.out.println("name: " + n);
} catch (Exception ex) {
Logger.getLogger(A.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
有什么想法吗?
【问题讨论】:
标签: java servlets properties