It's not a secret anymore!
The moose likes JSP and the fly likes display a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "display a "save as" dialog" Watch "display a "save as" dialog" New topic

display a "save as" dialog

Ste Lamb

Joined: Feb 05, 2003
Posts: 14
This is probably very easy to do but I can't find a way to do it.
I have some information displayed on a JSP which I want to save to a simple text file.
On selecting a button I want it bring up the "save as" dialog where the user can decide where to save the file to on their local machine. I can create the file and put data into it in Java, but I can't work out how to bring up the save as dialog from the JSP.
Any help much appreciated! Also any good resources for this sort of thing would also be appreciated.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63839

There is no standards-compliant method to do this that will work across all browsers. I wouldn't be surprised if there were an IE-only way to do this in that browser, but I wouldn't know. If this is the route that you wish to take, perhaps the HTML/Javascript forum would be a good place to further pursue this.
But I'd say that a better way to do this would be for your button to post a request to a servlet that would set the content type of the response to text and specify it as an 'attachment' (search these forums for this term for exact syntax examples) and then outputs the data in whatever text-only format is approriate.
This should cause the download dialog of the browser to open to allow the user to save the text file to disk.

[Asking smart questions] [About Bear] [Books by Bear]
Ste Lamb

Joined: Feb 05, 2003
Posts: 14
Bear thanks for your response.
I had finally worked it out using the method you suggest - although I'm requesting the info from another JSP. How would I reference a servlet directly?
Thanks for your response though, it doesn't seem to be documented anywhere that this is the way to do it.. altohugh more likely I was searching in the wrong place!
I agree. Here's the link:
subject: display a "save as" dialog
It's not a secret anymore!