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?
The WEB-INF/classes and WEB-INF/lib directories contain Java class files and JAR libraries, respectively. The WEB-INF/classes directory is automatically added to the classpath of the web application, so any class files placed here (using the normal Java package conventions) are available to the application.
The right place to put a JAR file to make its contents available to a Java web application at runtime is in the WEB-INF\lib directory of the WAR file in which the application is packaged.
These config files are typically placed under separate root directory than the rest of application code. For example, in case of Java they are typically under src/main/resources .
Web Application Directory Structure:JSP (Java Server Pages), which use to generate dynamic content. Servlets. Some External library or the jar files. Java utility classes.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With