A web container provides a context attribute that points to a temp directory:
Servlet spec SRV.3.7.1 Temporary Working Directories
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 ServletContext.TEMPDIR (javax.servlet.context.tempdir
) context attribute. The objects
associated with the attribute must be of type java.io.File.
Example :
File appTempDir = getServletContext().getAttribute(ServletContext.TEMPDIR);
File tempFile = File.createTempFile("process1", ".txt", appTempDir);
tempFile.deleteOnExit();
try {
...
} finally {
tempFile.delete();
}