tags:

views:

361

answers:

5

Where should I store persistent files in a Tomcat web app ?

  • javax.servlet.context.tempdir is not feasible, it's erased when the app is redeployed/removed
  • Don't want to use an absolute path in e.g. servlet init parameters
  • Storing the files in a database is not an option
+2  A: 

Answering the title of the question, what about using a database, a DataSource and JDNI? Even in a web only context, writing to files using java.io is not really recommended because of concurrency, threading, security, clustering, portability issues. Some of these problems can be "workarounded" but still, this is not really a best practice. The standard approach is to use a database and I'd suggest to reconsider this option, throwing "file-based" lightweight database like HSQLBD or JavaDB into the mix.

(EDIT: For an unknown reason, database is not an option. Using JNDI or context parameters or init parameters to pass an absolute path - which are the less worse options IMHO - is excluded too. For a relative path, maybe look at user.home or user.dir then - or any other system property that you could pass on the command line. I don't like it, I wouldn't do it, and this doesn't solve the issues previously mentioned, but it's your choice after all.)

Pascal Thivent
Claridfied. Database is not an option.
nos
A: 

I generally would suggest to use a database to store persistent data and expose it via a DataSource.

If you don't want to do that, I guess you could consider using the "user.home" system property (I have seen this used in a few circumstances). But... there are no guarantees that your servlet will be run with permission to write access unless you configure that yourself.

Suppressingfire
+2  A: 

Our team does this a lot. A general rule we follow is outside the web app and outside Tomcat.

Our sysadmin set up a directory on our server that the tomcat user has rw permissions to (e.g. /var/tomcat/persist). We have a built a directory structure under this that tomcat uses to store files, read app-specific init files, etc.

If you don't want to use an absolute path in your init-params for your servlet, consider setting a system property when tomcat is started up. The good thing about that is every application running under tomcat will have access to it. The bad thing about that is every application running under tomcat will have access to it. You could set a property named base.persist.dir and build subdirectories for each application underneath it. We set system properties in the setenv.sh script in the bin/ directory under the CATALINA_OPTS environment variable.

Andy Gherna
+1  A: 

Storing the files in a webapp directory under the home directory of the user running Tomcat is a good and convenient option. It is outside of Tomcan, which means it will survive redeployment, and it is usually a writable directory (ecause it is created under the users' home dir). But it is always a ggod idea to allow overriding the location of such directory via system property.

yossis
A: 

Generally, this would go to the database. But since the OP insists on not using a database, I'd try a different approach:

  • Filesystem path which is known: ${user.home}/.myapp. Applications sometimes use this for e.g. search indices which can be recalculated based on data in the database. Might be okay for your use case to use the user's home.
  • Store the configurable filesystem path in a configuration repository such as the database or perhaps Java Preferences (if you don't like to use servlet init params). Commercial applications such as Atlassian JIRA use a configurable (but absolute) filesystem path where they store issue attachments. If they don't know a better way, i don't know who does :)
mhaller