【发布时间】:2015-04-26 11:30:29
【问题描述】:
我正在使用 servlet、jsp 和 java 类构建 Web 应用程序。
这里是java结构
我想访问system.properties 文件,因为该文件正在存储项目配置。
我所做的是使用此 java 代码使用 FileInputStream 获取文件
/* generate the properties file objects */
Properties prop = new Properties();
/* generate IO to read from properties file */
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("system.properties");
if (inputStream != null) {
/* load the properties file */
prop.load(inputStream);
将文件中的所有变量放入对象中
host = prop.getProperty("host");
但它显示错误nullpointerexception,因为我知道属性文件中的参数未加载。
Apr 26, 2015 7:21:14 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.domain.servlet.TotalErrorSummaryServlet] in context with path [/MonitoringDashboard] threw exception
java.lang.NullPointerException
对编辑system.properties 文件的加载器有什么建议吗?当我将文件放在 WEB-INF 文件夹中时。
【问题讨论】:
-
将
system.properties放入WEB-INF/classes并在getResourceAsStream("/system.properties")中添加/ -
试试
InputStream inputStream = class.getClass().getClassLoader().getResourceAsStream("WEB-INF/system.properties");,而不是你的inputStream。 -
如果属性文件不可访问你会得到一个异常
prop.load()你能在getProperty之后记录host的值吗?
标签: java jsp servlets properties