为什么我在独立的 tomcat 实例中访问 .properties 文件时遇到问题,但在 eclipse 嵌入式实例中却无法访问?

发布于 2024-09-13 16:58:59 字数 1123 浏览 9 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ζ澈沫 2024-09-20 16:58:59

好吧,经过多次浏览,我发现似乎“正常”的为什么要做我想做的事情:

而不是......(我是如何做的)

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

修复

public class PropertyLoader { 

    public static final Properties bundle = new Properties();

    static { 

        InputStream stream = null;
        stream = SBOConstants.class.getResourceAsStream("/sbonline.properties");
        bundle.load(stream);

    } 

}//End of class

我正在修改别人的代码,所以我不确定他们为什么首先以另一种方式这样做...但我想 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)

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

THE FIX

public class PropertyLoader { 

    public static final Properties bundle = new Properties();

    static { 

        InputStream stream = null;
        stream = SBOConstants.class.getResourceAsStream("/sbonline.properties");
        bundle.load(stream);

    } 

}//End of class

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文