views:

1412

answers:

6

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?

+2  A: 

I would not store it in the application folder, because that would override the configuration with a new deployment of the application.

I suggest you have a look at the Preferences API, or write something in the users folder (the user that is running Tomcat).

Hans Doggen
+1  A: 

The answer to this depends on how you intend to read and write that config file.

For example, the Spring framework gives you the ability to use XML configuration files (or Java property files); these can be stored in your classpath (e.g., in the WEB-INF directory), anywhere else on the filesystem, or even in memory. If you were to use Spring for this, then the easiest place to store the config file is in your WEB-INF directory, and then use Spring's ClassPathXmlApplicationContext class to access your configuration file.

But again, it all depends on how you plan to access that file.

delfuego
+1  A: 

If it is your custom config WEB-INF is a good place for it. But some libraries may require configs to reside in WEB-INF/classes.

axk
+8  A: 

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.

MBCook
This works for us too, but it can be a pain if you occasionally deploy on Windows, where it isn't natural to add /opt and the like.
Peter Hilton
Rather than referencing the full path to the file you can put the "config" directory on the classpath for the server and just use ClassLoader.getResource().
John Meagher
We specify a system property when tomcat starts up. Our app gets our config directory from this system property, so we can use a different configuration directory from server to server.
ScArcher2
Thanks for your suggestions. They are very usefull to me.
Frans
+7  A: 

WEB-INF is a good place to put your config file. Here's some code to get the absolute path of the directory from a servlet.

public void init(ServletConfig servletConfig) throws ServletException{
    super.init(servletConfig);
    String path = servletConfig.getServletContext().getRealPath("/WEB-INF")
Jataro
+1  A: 

Putting it in WEB-INF will hide the XML file from users who try to access it directly through a URL, so yes, I'd say put it in WEB-INF.

Michael Angstadt