开发者

Keeping a settings file in a Web Application

开发者 https://www.devze.com 2023-01-12 08:15 出处:网络
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 sett

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.


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.


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.


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 Java EE standard.
  • con: It works after servlet starts up.


Keeping settings in database is better as the application needs to support clustering

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号