tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <>
Subject Re: Wanted: directory for persistent file storage for webapp
Date Wed, 24 May 2000 23:23:27 GMT wrote:

> Let's say I have a guest log servlet. Users can come in and write
> messages; the messages get saved to a file; the servlet loads in the
> file and displays the content inside itself.
> Easy, right?
> Back before webapps, it was easy. I could use getRealPath() or some
> such and write the file in the servlet's directory, or in the web
> document directory, or in some hardcoded location.
> But with webapps, where you can deploy a WAR on an unknown server,
> there's no platform-independent way to discover a persistent work
> directory -- or is there?
> A related issue is, how to write *web* accessible content. That is, if
> I have a servlet that writes a GIF, where do I save it so that a web
> browser can access it? Especially if I want to save it so a URL with
> the context-string attached (/application/images/new.gif).
> Again, I know there are ways to hack this in on a per-server basis,
> probably using init parameters. But does the Servlets 2.2 spec address
> these issues at all? I've read it through and it doesn't seem to.
>  - Alex

The standard answer to persistent storage is "use databases, or directory
servers, or EJBs for stuff like that."

If you really want to use disk files (stubborn, aren't we :-), the best
approach is to use an initialization parameter in the deployment descriptor to
tell you where the persistent directory is.  The servlet context does provide
you a context attribute with a work directory
("javax.servlet.context.tempdir"), but that is primarily for temporary use --
there are no guarantees of persistence.

If you really don't want to use an initialization parameter, the following
kludge will sometimes work:

    String pathname = getServletContext().getRealPath("/") +

and you can then write to a file in the /WEB-INF subdirectory.  However, this
will not work on any servlet container that runs your application directly
from a WAR file -- in such cases, getRealPath() will return null.  It also
will not work, of course, if your servlet container only gives you read access
(instead of read/write access) to the directory in which your application is

Craig McClanahan

View raw message