Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best place to store a configuration file in a Java web application (WAR)?

I create a web application (WAR) and deploy it on Tomcat. In the webapp there is a page with a form where an administrator can enter some configuration data. I don't want to store this data in an DBMS, but just in an XML file on the file system. Where to put it?

I would like to put the file somewhere in the directory tree where the application itself is deployed. Should my configuration file be in the WEB-INF directory? Or put it somewhere else?

And what is the Java code to use in a servlet to find the absolute path of the directory? Or can it be accessed with a relative path?

like image 986
boes Avatar asked Sep 18 '08 19:09

boes


People also ask

Where should config file be located?

Generally system/global config is stored somewhere under /etc. User-specific config is stored in the user's home directory, often as a hidden file, sometimes as a hidden directory containing non-hidden files (and possibly more subdirectories).

Where should web XML be stored in a WAR file?

Auxiliary JARs (tag libraries and any utility libraries called by server-side classes) must be stored in the WEB-INF/lib directory. web. xml—the web component deployment descriptor is stored in the WEB-INF directory.

Which directory is used to store configuration files?

Most global config files are located in the /etc directory. The /etc/ directory feels more like a filesystem and has many sub-directories, each having related config files.


1 Answers

What we do is to put it in a separate directory on the server (you could use something like /config, /opt/config, /root/config, /home/username/config, or anything you want). When our servlets start up, they read the XML file, get a few things out of it (most importantly DB connection information), and that's it.

I asked about why we did this once.

It would be nice to store everything in the DB, but obviously you can't store DB connection information in the DB.

You could hardcode things in the code, but that's ugly for many reasons. If the info ever has to change you have to rebuild the code and redeploy. If someone gets a copy of your code or your WAR file they would then get that information.

Putting things in the WAR file seems nice, but if you want to change things much it could be a bad idea. The problem is that if you have to change the information, then next time you redeploy it will overwrite the file so anything you didn't remember to change in the version getting built into the WAR gets forgotten.

The file in a special place on the file system thing works quite well for us. It doesn't have any big downsides. You know where it is, it's stored seperatly, makes deploying to multiple machines easy if they all need different config values (since it's not part of the WAR).

The only other solution I can think of that would work well would be keeping everything in the DB except the DB login info. That would come from Java system properties that are retrieved through the JVM. This the Preferences API thing mentioned by Hans Doggen above. I don't think it was around when our application was first developed, if it was it wasn't used.

As for the path for accessing the configuration file, it's just a file on the filesystem. You don't need to worry about the web path. So when your servlet starts up it just opens the file at "/config/myapp/config.xml" (or whatever) and it will find the right thing. Just hardcodeing the path in for this one seems pretty harmless to me.

like image 141
MBCook Avatar answered Sep 25 '22 08:09

MBCook