aspose file tools*
The moose likes Servlets and the fly likes Serving from temp or other directory? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Serving from temp or other directory?" Watch "Serving from temp or other directory?" New topic
Author

Serving from temp or other directory?

Ben Snyder
Greenhorn

Joined: Jul 02, 2002
Posts: 6
Howdy folks.

I have some HTML and image files which I want to serve from a web application's temp directory. Which of course changes with each session.

Is there a sane way to do this, or is there another facility where I can set up a static file directory on each session creation?

Here's the long story:

Server is Tomcat. I have a web app with servlets which process data. Final step is done by a system call - this cannot be changed. The files created here are relative to the session and cannot be accessed from a different session. But they need to be viewable directly from another servlet, inserted as an iframe. This is also non-negotiable as it's the spec for this app.

Thanks!

-ben
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

you should keep things wrapped up in your own application directory structure and try not to go outside this.
The contents of the WEB-INF directory are protected from being directly accessed by the client, so it is common to have a WEB-INF/temp or WEB-INF/upload directory
Ben Snyder
Greenhorn

Joined: Jul 02, 2002
Posts: 6
David O'Meara wrote:you should keep things wrapped up in your own application directory structure and try not to go outside this.
The contents of the WEB-INF directory are protected from being directly accessed by the client, so it is common to have a WEB-INF/temp or WEB-INF/upload directory


Since the content is dynamically generated, and the app is packaged as a war file, I'm having difficulty seeing how this would work. Plus these files are session specific...

-ben
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Then I may have misunderstood the problem.

As I understand it, a request within a session runs an external process and resources get created in a location specific to that session.
If the problem is where to write the info, use the java.io.tempdir system property
If you want manage creation and deletion of these resources, use context and session listeners
If you want a unique directory, use the session ID

I'm not sure what the issue is, beyond this.
Ben Snyder
Greenhorn

Joined: Jul 02, 2002
Posts: 6
The problem is not creating session-specific directories and files, the problem is actually serving them to the client.

Basically, I am using java.io.tempdir to create a directory. Then, I rely on a system call to populate this directory with some text files, image files, and one HTML file.
What I would then like to do, is have a URL that can be used to actually serve this HTML file...I will be embedding this URL into an IFRAME. Though it shouldn't matter, I just need the url.

Make sense?

-ben
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Ah now.
Create a servlet mapped to (for example) /userdata/* then use the path data from the request to find the file name, then pump the data from the session directory to the output stream.

eg /userdata/icon.gif detects the additional 'icon.gif' and converts that to new File(java.io.tempdir+"/"+sessionId+"/"+"icon.gif") then open that file and send to the client
If I had some sample code available I'd show you, but it is important to ensure that the actual file opened is under the java.io.tempdir directory and that people aren't able to use it to access system files.
Ben Snyder
Greenhorn

Joined: Jul 02, 2002
Posts: 6
*Very* cool, thanks! This should do it!

-ben
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Good luck, be sure to drop back and tell us how you go!
 
jQuery in Action, 2nd edition
 
subject: Serving from temp or other directory?