Load properties file in a java servlet deployed in JBoss as a war

后端 未结 4 703
你的背包
你的背包 2021-01-02 12:03

I have a servlet deployed as a war in JBoss 4.0.2. I have a properties file for the deployed application. Where should I put this file? Under the conf directory in the jbo

4条回答
  •  鱼传尺愫
    2021-01-02 12:30

    If the properties file can be deployed along with the application make it part of your source tree. This will result in the properties file to be in the WEB-INF/classes folder.

    This can then be read using

    Properties properties = loadProperties("PropertyFileName.properties", this.getClass());
    ...
    
    public static Properties loadProperties(String resourceName, Class cl) {
        Properties properties = new Properties();
        ClassLoader loader = cl.getClassLoader();
        try {
            InputStream in = loader.getResourceAsStream(resourceName);
            if (in != null) {
                properties.load(in);
            }
    
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }
    

提交回复
热议问题