为什么我在独立的 tomcat 实例中访问 .properties 文件时遇到问题,但在 eclipse 嵌入式实例中却无法访问?
我在 Eclipse 中编写了一个简单的 Hello World Servlet,在 HelloWorldServlet.java 的 doGet 方法中包含以下内容
PrintWriter writer = response.getWriter();
String hello = PropertyLoader.bundle.getProperty("hello");
writer.append(hello);
writer.flush();
。 PropertyLoader 是一个与 Servlet 位于同一包中的简单类,它执行以下操作:
public class PropertyLoader {
public static final Properties bundle = new Properties();
static {
InputStream stream = null;
URL url = PropertyLoader.class.getResource("/helloSettings.properties");
stream = new FileInputStream(url.getFile());
bundle.load(stream);
}
}//End of class
我在 /WebContent/ 中放置了一个名为 helloSettings.properties 的文件WEB-IND/classes 包含以下单行内容:
hello=Hello Settings World
当我将 Tomcat 6.0 添加到我的项目并在 eclipse 中运行它时,它成功地将
“Hello Settings World”打印到 Web 浏览器。
但是,当我将项目导出为 war 文件并手动将其放入 .../Tomcat 6.0/webapps 然后我得到“null”作为我的结果。
是类路径/类加载器配置的问题吗?权限?还有其他配置文件吗?我知道 helloSettings.properties 文件位于 WEB-INF/classes 文件夹中。
有什么帮助吗?
I wrote a simple Hello World Servlet in Eclipse containing the following in the doGet method of my HelloWorldServlet.java
PrintWriter writer = response.getWriter();
String hello = PropertyLoader.bundle.getProperty("hello");
writer.append(hello);
writer.flush();
PropertyLoader is a simple class in the same package as the Servlet that does the following:
public class PropertyLoader {
public static final Properties bundle = new Properties();
static {
InputStream stream = null;
URL url = PropertyLoader.class.getResource("/helloSettings.properties");
stream = new FileInputStream(url.getFile());
bundle.load(stream);
}
}//End of class
I placed a file called helloSettings.properties in /WebContent/WEB-IND/classes that contains the following single line of content:
hello=Hello Settings World
When I add Tomcat 6.0 to my project and run it in eclipse it successfully prints
"Hello Settings World" to the web browser.
However when I export the project as a war file and manually place it in
.../Tomcat 6.0/webapps I then get "null" as my result.
Is it a problem with the classpath/classloader configuration? permissions? any of the other configuration files? I know for a fact that the helloSettings.properties file is in the WEB-INF/classes folder.
Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,经过多次浏览,我发现似乎“正常”的为什么要做我想做的事情:
而不是......(我是如何做的)
修复
我正在修改别人的代码,所以我不确定他们为什么首先以另一种方式这样做...但我想 url.getFile() 是我的问题,我不知道为什么。
Well, after much browsing I found what seems a "normal" why to do what I'm trying to do:
Instead of...(how I was doing it)
THE FIX
I'm modifiying someone else's code so I'm not sure why they did it the other way in the first place... but I guess
url.getFile()
was my problem and I don't know why.