Search code examples
tomcatservletsfilesystemsderby

Acceptable locations for a servlet to write to?


I would like to embed a Derby database in a servlet-based application running on Tomcat.

For this purpose, I need to persist the database files on the disk. I would prefer for them to reside in a sub-folder of the deployed web application (which I could then convert using getServletContext().getRealPath()).

Is there a good practice wrt acceptable, application-relative locations where a servlet could safely write to? If no such recommendation exists, what would be a tomcat-specific good practice?

This discussion seems to rule out writing in WebContent; will I have to write to a CATALINA_HOME sub-folder, as some tutorials point to (but it feels hackish)?


Solution

  • You need to make the filesystem persistence location an explicit parameter to your program, probably as a property (i.e. -DdataStoreLocation=somepath). In general, a Java application may or may not have access to a filesystem -- it could be running from within a WAR or even have been loaded from the network and have no classpath-relative filesystem.

    Making it an explicit requirement and part of your app's contract with the user is the only way to guarantee that there's a filesystem to use, and a way to get the path name to use.