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

后端 未结 4 740
你的背包
你的背包 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:34

    To load that properties file in a portable manner, the best way would be to put it on the classpath of the web application (either in a JAR under WEB-INF/lib/ or under WEB-INF/classes/ or on the app server classpath if you want to be able to edit that file without repackaging your web application) and to use Class#getResourceAsStream(String).

    The following code gets an InputStream for a property file which resides in the same package as the servlet in which the code is executed:

    InputStream inStream = Thread.currentThread().getContextClassLoader()
                     .getResourceAsStream("myfile.properties");
    

    Then, load(InputStream) it into a Properties object (skipping Exception handling):

    Properties props = new Properties();
    props.load(inStream);
    

提交回复
热议问题