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

I am building a web application (WAR) and deploying it to Tomcat. The webapp has a page with a form in which the administrator can enter some configuration data. I do not want to store this data in the DBMS, but only in the XML file in the file system. Where to say?

I would like to place 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 in another place?

And what Java code is used in the servlet to find the absolute path to the directory? Or can one access with a relative path?

+53
java java-ee web-applications tomcat
Sep 18 '08 at 19:53
source share
6 answers

We do this to put it in a separate directory on the server (you can use something like / config, / opt / config, / root / config, / home / username / config or whatever). When our servlets start, they read the XML file, extract a few things from it (most importantly, information about connecting to the DB) and what it is.

I asked why we did it once.

It would be nice to store everything in the database, but obviously you cannot store information about connecting the database to the database.

You can hardcode the code in code, but this is ugly for many reasons. If the information ever needs to change, you need to rebuild the code and redeploy. If someone receives a copy of your code or your WAR file, they will then receive this information.

Putting things in a WAR file seems nice, but if you want to change a lot, it might be a bad idea. The problem is that if you need to change the information, then the next time you redeploy it, it will overwrite the file so that everything that you do not remember is forgotten to change the version built into the WAR.

A file in a special place on the file system works pretty well for us. He has no big flaws. You know where it is located, it is stored separately, it simplifies deployment on several machines if they all need different configuration values โ€‹โ€‹(since they are not part of the WAR).

The only other solution that I can think of that this will work well is to store everything in the database, except for the entry information in the database. This comes from Java system properties that are retrieved through the JVM. This is the settings API mentioned by Hans Doggen above. I donโ€™t think it was around when our application was first developed if it werenโ€™t used.

As for the access path to the configuration file, it is just a file in the file system. You do not need to worry about the web path. So when your servlet starts up, it just opens the file in the /config/myapp/config.xml file (or something else) and it will find the right thing. Just hardcodeing the way for this seems completely harmless to me.

+47
Sep 18 '08 at 20:15
source share

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

public void init(ServletConfig servletConfig) throws ServletException{ super.init(servletConfig); String path = servletConfig.getServletContext().getRealPath("/WEB-INF") 
+17
Sep 18 '08 at 20:19
source share

By entering it in WEB-INF , hide the XML file from users who are trying to access it directly through the URL, yes, I would say, put it in WEB-INF .

+10
Sep 18 '08 at 20:29
source share

I will not store it in the application folder, because it will override the configuration when the application is redeployed.

I suggest you familiarize yourself with the Preferences API or write something in the users folder (the user who runs Tomcat).

+4
Sep 18 '08 at 19:57
source share

The answer to this question depends on how you are going to read and write this configuration file.

For example, the Spring framework makes it possible to use XML configuration files (or Java property files); they can be stored in your class path (for example, in the WEB-INF directory), somewhere else in the file system, or even in memory. If you want to use Spring for this, then the easiest place to store the configuration file is in your WEB-INF directory, and then use Spring ClassPathXmlApplicationContext to access your configuration file.

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

+3
Sep 18 '08 at 19:57
source share

If this is your custom configuration, WEB-INF is a good place for it. But some libraries may require configuration files to be in WEB-INF / classes.

+1
Sep 18 '08 at 19:58
source share