Reputation: 6472
My Java web application (myapp.war
) ist deployed by placing it into the webapps
direcotry on Tomcat on Ubuntu 10.04.
This application needs to save some data in files. But the user, which is running Tomcat (tomcat6
) has no write access to the home directory /usr/share/tomcat6/
and no write access to the current working direcotry /var/lib/tomcat6/
, since both belong to root
.
So where should a web application store its data? I hope it is not the extracted archive in the webapps direcotry. This one could be deleted very easily by accident. And Tomcat can be configured, not to extract .war files. Then there would be no extracted direcotry.
Perhaps /var/lib/tomcat6/
should belong to user tomcat6
ant this is a bug in Ubuntu?
Upvotes: 18
Views: 9819
Reputation: 11239
If the files need not persist longer than the life-cycle of the servlet context, the servlet container provides a private temporary directory for each servlet context, specified by javax.servlet.context.tempdir
attribute.
See Servlet Specification 2.3, Chapter 3 Servlet Context
3.7.1 Temporary Working Directories
The convenience of a temporary storage directory is required for each servlet context. Servlet containers must provide a private temporary directory per servlet context and make it available via the javax.servlet.context.tempdircontext attribute. The object associated with the attribute must be of type java.io.File
Upvotes: 4
Reputation: 14830
Answering his own question, Witek stated /var/lib/tomcat6/webapps/ is writable
-- at least on his installation of his version of Ubuntu. On my RHEL 5.2 system /var/lib/tomcat<X>
doesn't even exist, so there is no webapps subdirectory writable or not, which leads to my answer.
Q: Where should a Java web application store its data?
A: Wherever you've configured it to store its data.
Make the location configurable, in web.xml as a <context-param>
or in a myApplication.properties file.
Upvotes: 3
Reputation: 6472
I found the solution on Launchpad. /var/lib/tomcat6/webapps/
is writable. This means, that the following works:
File myFile = new File("webapps/myfile.txt");
Upvotes: 2
Reputation: 40186
I think it depends on what kind of data you are dealing with. Most of the time, data goes into the database simply because it is fast and easy to perform a CRUD. If you want to store localized user configuration and you don't care how portable it is, perhaps you can store under user.home, I did that for one of my projects and that works fine. All that being said, I really don't think there's any best practice on this and database seems to be the most obvious choice because you can do whole lot of different tasks against it, and most of them are free to begin with. :)
Upvotes: 2
Reputation: 43610
I haven't seen any specific guidance on where you should store that kind of data locally - probably because you'd normally store that kind of data in a database.
When I need to store quick-and-dirty data like that, I store it on a filesystem specifically for that data, to isolate it from other areas where it might get caught up in other activity. Haven't had any issues with the approach so far. Also, if it's important, make sure you store it somewhere where you're backing it up :)
Upvotes: 0