Hi David,
>
> I would like to generate a custom text file for my users to download.
> Currently, I pop up a window with the text, and tell them to copy it and paste
> it into a text file. But I would like instead to have a link that they can
> click to download a file.
>
> Is it possible to create a file from Java to do this? And then is it possible
> to delete the file when the session expires?
The method createTempFile in java.io.File can be used to create a temporary file,
but the file isn't removed automatically. This can be done with deleteOnExit in
the java.io.File object retured by createTempFile, which is not the best idea in
servlets and JSPs because it may take a while until the virtual machine terminates,
depending on the usual uptime of your server. You could add a helper class implementing
the javax.servlet.http.HttpSessionBindingListener-Interface (with the java.io.File
object
of the temporary file as only member) to the session and call delete on the File object
when the session calls valueUnbound in the helper object.
You have to put the temporay file into a directory available over the webserver,
the usual /tmp or C:\TMP won't do. And make sure you are allowed to create files
in that directory.
You could also write a servlet or JSP which generates the custom text file from given
parameters and then put a link including the required parameters to that servlet in the
document. The user then can download the custom text file directly from the generating
servlet (set the content-type to "text/plain" in the response), which means you won't
need a temporary file on the server. Furthermore a servlet can check the session and
authentication of the user before it provides the data.
Chris
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets