views:

72

answers:

3

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.

+2  A: 

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.

Soundlink
You'll only lose the logs whenever you redeploy the webapp.
BalusC
That doesn't bother me. The logs end up going to the tomcat logs folder [which doesn't bother me]
monksy
Is the Web-INF compiled into the war or is that outside the war?
monksy
If it is compiled in to the WAR I need a way to have it outside of the application.
monksy
A: 

Agreed with Soundlink.

You also could store your setting in database. When your servlet starts up, the setting data should be loaded from database.

  • pro: No need to maintain configuration files which are not jee standard.
  • con: It works after servlet starts up.
卢声远 Shengyuan Lu
I don't like this solution because it requires for you to have a specific set of tables in the database, and or a database for the application. The database I'm working with is already quite large. Setting files make the web app abit easier to manage.
monksy
+1  A: 

Just add the path to the configuration files to the webapp's classpath. That's the common approach and many API's are also designed to load the configuration from the classpath. For the case you're loading those files manually, you can use ClassLoader#getResourceAsStream() to get an InputStream of the classpath resource.

First, create a fixed path somewhere outside the webapp directory to drop the configurationfiles in, e.g. /var/webapp/config. Then configure the servletcontainer to include this path in the classpath. In case of Tomcat, you can specify it in the shared.loader or common.loader property of the /conf/catalina.properties file.

BalusC
If your are doing it this way, the configuration files are visible to all installed webapps. If you have only one webapp, that's okay, otherwise this could be problematic.
Soundlink