Search code examples
javatomcatjakarta-eeconfiguration-files

Keeping a settings file in a Web Application


Are there any recommendations on how to keep an application level settings/configuration file in a J2EE project? I have a web project that has dependences that must write to the logs, and a local settings file. Is there anyway to store the settings file in the local directory and have it writable?

[Some of the dependencies are: hibernate and gridgain, so they produce a lot of logs]

I would rather not hard code the location of the settings file.


Solution

  • You should put the configuration files under the WEB-INF folder. You could create a directory /WEB-INF/config. If they should be in the classpath put them to /WEB-INF/classes. By doing it this way, the location is not hardcoded, but at a well known place for your application.

    Putting them inside /WEB-INF is important, so the files are not "accessible" by the browser.


    Another way could be to add a parameter to the web.xml configuration, where you specify the location of your configuration files so they are not inside the webapps directory. You could do this by adding a context-param. For example:

    <context-param>
        <param-name>configuration</param-name>
        <param-value>/var/myapp/config</param-value>
    </context-param>
    

    The value will be made visible to your web application as a servlet context initialization parameter named configuration. You can then use it as a base path to read your configuration files (Get the value by calling the getInitParameter method from the ServletContext).

    But this will only work for config files, which are not needed in the classpath.