Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should a Java web application store its data?

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?

like image 854
Witek Avatar asked Jul 01 '10 17:07

Witek


People also ask

Where can you keep Java files in web application?

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.

In which directory on a web application where we can put a jar file?

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.

Where the configuration files are stored in your application in Java?

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 .

What is the structure of a Java web application?

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.


2 Answers

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

like image 112
2 revs Avatar answered Sep 21 '22 05:09

2 revs


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.

  • I can put it where I want on my box, the SysAdmins can put it where they want on the production system.
  • You can change your mind later.
  • You don't need symbolic links (which have magically disappeared on me before, breaking the system.)
  • You can have several sets of test data, and just point the configuration at whichever one you want.
  • You can put it wherever there's disk space.
  • You are going to change your mind later.
like image 44
Stephen P Avatar answered Sep 20 '22 05:09

Stephen P