我正在尝试读取 WEB-INF 中的属性文件,但我得到的只是空的。我对 Java Web 开发完全陌生。
我的初始化方法:
 @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;
    }
我的 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>
我知道我一定遗漏了一些东西,因为 String getDataBaseUrl 返回 null。请帮忙。