使用init读取WEB-INF文件夹java中的属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18894129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:00:59  来源:igfitidea点击:

Read Properties file in WEB-INF folder java using init

javaservletsproperties

提问by ErrorNotFoundException

I am trying to Read a properties file inside WEB-INF but all I am getting is null. I am completely new to java web development.

我正在尝试读取 WEB-INF 中的属性文件,但我得到的只是空值。我对 Java Web 开发完全陌生。

My init method:

我的初始化方法:

 @Override
    public void init() throws ServletException {
        try {
            String path = "";
            path = "/WEB-INF/" + getServletContext().getInitParameter("monpesapropertiesfile");
            String realPath;
            realPath = getServletContext().getRealPath(path);
            if(realPath != null){
                 InputStream fis = new FileInputStream(realPath);
                 prop.load(fis);
            }
        } catch (Exception asd) {
            System.out.println(asd.getMessage());
        }
    }
    public String getDatabaseUrl() {
        String url = prop.getProperty("database.url");
        return url;
    }

My Web.xml:

我的 Web.xml:

<context-param>
        <param-name>monpesapropertiesfile</param-name>
        <param-value>monpesaproperties.properties</param-value>
    </context-param>
    <servlet>
        <servlet-name>ReadProperty</servlet-name>
        <servlet-class>com.monpesa.properties.ReadProperty</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

I know I must be missing something because the String getDataBaseUrl Returns null. Please help.

我知道我一定遗漏了一些东西,因为 String getDataBaseUrl 返回 null。请帮忙。

采纳答案by acc15

Check this out:

看一下这个:

http://docs.oracle.com/cd/E17802_01/products/products/servlet/2.5/docs/servlet-2_5-mr2/javax/servlet/ServletContext.html#getResource(java.lang.String)

http://docs.oracle.com/cd/E17802_01/products/products/servlet/2.5/docs/servlet-2_5-mr2/javax/servlet/ServletContext.html#getResource(java.lang.String)

<context-param>
    <param-name>monpesapropertiesfile</param-name>
    <param-value>/WEB-INF/monpesaproperties.properties</param-value>
</context-param>

public void init() throws ServletException {
    try {
        String path = getServletContext().getInitParameter("monpesapropertiesfile");
        final InputStream is = getServletContext().getResourceAsStream(path);
        try {
            prop.load(is);
        } finally {
            is.close();
        }
    } catch (Exception asd) {
        System.out.println(asd.getMessage());
    }
}

回答by Thilo

I would either

我要么

1) put the properties into WEB-INF/classesand use getClass().getResourceAsStream()to load it.

1)将属性放入WEB-INF/classes并使用getClass().getResourceAsStream()加载它。

or

或者

2) put the full file path into web.xml (and place the file outside of the web application into a user-editable configuration area)

2) 将完整的文件路径放入 web.xml (并将 web 应用程序之外的文件放入用户可编辑的配置区域)