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

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

    The best place to put it is under the web-apps' own doc-root, like "./WEB-INF/myapp.properties", i.e. relative to where the servlet container unpacked your .war or .ear file. You can provide the properties file directly in the .war.

    The ServletContext has a method getRealPath(String path) that returns the actual path in the filesystem. Using the real path you can load it in a Properties collection.

    Update The code in your comment tries to lookup real path for "/", you should ask for the relative path to your properties file, as in:

    String propertiesFilePath = getServletContext().getRealPath("WEB-INF/application.properties");
    Properties props = properties.load(new FileInputStream(propertiesFilePath));
    

提交回复
热议问题