Keeping a settings file in a Web Application

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.




4 Answers
4



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.


WEB-INF


/WEB-INF/config


/WEB-INF/classes



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


/WEB-INF



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:


web.xml


context-param


<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).


getInitParameter


ServletContext



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





You'll only lose the logs whenever you redeploy the webapp.
– BalusC
Aug 22 '10 at 4:16





That doesn't bother me. The logs end up going to the tomcat logs folder [which doesn't bother me]
– monksy
Aug 22 '10 at 4:53





Is the Web-INF compiled into the war or is that outside the war?
– monksy
Aug 22 '10 at 4:54





If it is compiled in to the WAR I need a way to have it outside of the application.
– monksy
Aug 22 '10 at 5:39



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.


ClassLoader#getResourceAsStream()


InputStream



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.


/var/webapp/config


shared.loader


common.loader


/conf/catalina.properties





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
Aug 22 '10 at 8:36



Agreed with Soundlink.



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





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
Aug 22 '10 at 4:48



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



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?