JSP: save a file on client without storing it on server - java

In my jsp someone wants to export a query result to csv. Can't use frameworks. At the moment, here is the process:
press button, go to servlet
prepare data for csv
make csv and save to server
back to jsp and let them download the fresh-made file from an anchor tag.
Thing is, I' don't want to create this file on server, as I have to dispose it afterwards, but I still want to give the user the "save as" window. So, is there a way, putting for example an OutputStream object in session, to achieve this result?
Thank you all!

A servlet can generate any type of content. So, when you click the button to run the servlet, simply have the servlet write the file back to the client at that time. You'll need to set the Content-type header to "text/csv" (and making sure that you set encoding properly). You don't need to set the Content-Length header; the browser can deal with that.
When the servlet returns data to the browser, the user will be prompted to save the file or open it with an application.

Yes. You don't need to save the file. Just hold it in the session and send it in-memory to the OutputStream. I would trash it after a given time or after delivery in order to save memory.
Set the content type and the content disposition appropriately. This will mean that the browser interprets the output correctly and prompts your user to save it or launch the appropriate application.
See this SO answer for more details.

Related

Dialog box in Spring MVC controller

Is there a way to display a (preferably modal) dialog box in an servlet controller ? If it isn't modal that is something I can deal with (!?) as long as it initially appears above the browser.
Essentially I have a form with a table and 2 buttons on it. One button takes the user to a different place in the workflow and is irrelevant to this question (just to explain why the other one doesn't 'go anywhere').
The other button currently goes back to an MVC controller, calls some code to export the table to excel and then reloads the web page. This is all working okay except the way I am calling the dialog box is calling it underneath the browser. I suspect this is because I am sending null as the frame but I'm not sure what to put in its place ?
JOptionPane.showMessageDialog(null, "Export Completed.", "Excel Export", JOptionPane.INFORMATION_MESSAGE);
Many thanks.
I am not too sure what you are trying to achieve from your question and without any code examples. However, presumably what you are doing (guessing here), is that you are trying to export some data from some data source and convert it to an Excel file. You have to keep 3 things in mind.
Web applications work via HTTP requests and responses. So the only thing a Servlet can do is send back an HTTP response that indicates that the export was successful. Whether you use a traditional page, or maybe use AJAX to avoid refreshing the page is purely your design choice. You could also start with a simple page and then change to AJAX combined with JQuery later once you get used to what is happening.
Exporting the excel sheet to the server does not mean that your client has access to it. Speculating here, but you will probably need a mechanism for your user to get the file. One simple approach used by many webapps is to actually send the Excel file as the Servlet response itself. So what would happen is that when the export is completed the browser starts receiving the file and the user sees it downloading. From your servlet you will just need to set the right mime-type and set the content-disposition header to state that the file is an attachment (so that the browser downloads it as a file).
httpresp.setContentType("text/csv");
httpresp.setHeader("Content-disposition", "attachment; filename=\"export.csv\"");
You will probably also need to set the file size. There are various full examples on SO if you look for further details.
When performing these operations remember that users can interrupt the browser or refresh. If the user presses F5 he might cause your application to do the export again. One common approach to this is called 'redirect-after-post'. Basically you redirect the user to a page which just displays the outcome, without performing the operation again. This way if he presses refresh, he is just refreshing the page with the message.

Downloading generated excel file using ajax

I am developing an application using struts2 framework. When user provides date range, I need to prepare the reports excel file and show that in download pop-up. I am able to create io stream of excel, but I am not getting how can I open that pop-up using ajax.
I am generating file on the fly and returns the stream. Therefore not able to give file path. I want ajax call to show some message like "Please wait..". Because file generation takes more time and it looks like link is not working..
There's nothing Ajax about downloading a file.
Your request can be an ajax request, but simply give the generated file path and the browser will do the downloading.
No need to perform any AJAX calls.
You just need to return the byte[] as a Struts2 Stream result,
configured with a ContentDisposition: attachment; (and not ContentDisposition: inline, that will try to open it inside the browser instead of asking about downloading or opening with a desktop application).
To notify the user that something is going on, that the request is sent and the system is not frozen, you need an Loading OVERLAY.
An Overlay is an element that is placed over your page, generally with a partially transparent background, an animated image saying "Loading..." , and a modal behavior (it won't close until the page is changed, and it will prevent double post of the same request, like double clicks etc...).
Of course if you open an attachment, the page will not change, then you'll need to intercept the end of the downloading and close the overlay by yourself, or give the user a button to close it.
To intercept it, you can try the Struts2 Execute and Wait Interceptor.
If you want to create a custom overlay, you can generate your Loading images with the ajaxload.info Generator.
If you instead don't want to reinvent the wheel, you can take a look at existing overlays, like those from jQuery TOOLS.
On the success method of ajax do not return the stream, instead save the file on server and open a pop up with address to an action that returns correct file. But pop ups might be blocked in some browsers so you need to allow that for your site (local host or other URL)

Creating files under tomcat

I want to display a file from DataBase (stored as Blob).so for that i want to copy it under tomcat Server after that call the method that shows the file .
So is there a possibility to create a temporary folder in tomcat .
Any help will be appreciated
Many thanks
What you want is a Java Servlet which:
Retrieves the data blob from the database
Streams the data back to the browser as though a file were being returned
Here's an example from over on Java Ranch that I think summarizes the idea pretty well: http://www.coderanch.com/t/291337/JSP/java/Display-database-BLOB-jsp
Just remember that the servlet is tied to a URL, it receives a set of parameters via the query parameters on the end of the URL, and then it will use those to go get the data and return it. Make sure that the MIME type on the returned data is correct because that is the browser's clue what to do with the data streamed to it. That is, to the browser, the response is just a big bunch of data and the MIME type it sees with it helps it determine what action it should take. Should it be saved to a file, displayed, etc.
You can see some discussion of that portion of things in this Stack Overflow question: Help getting image from Servlet to JSP page
There already exists a temporary folder in tomcat.
tomcat/temp
Regards

Opening a file on click of a link on a jsp page

I have a link(say open file) on a JSP page. On clicking that link, I need to open a file. The location of the file is present in a table on the Database. Since "onclick" event is a Javascript event, I am clueless on how to obtain the file location. Pls help. Im relatively new to this stuff. Pls give an idea to implement this. I can proceed based on the idea
On click call the servlet which reads the fie location from DB and redirect the user to that file location
or read the file and write it out to response and set the headers
That's an easy question.
Basically you must have a form that post's to a jsp page or servlet.
There you should read a hidden element that will point to the file.
There you should write the file on the response and send it back to the user.
See my post How to offer download of local PDF file in Java? for more info

Informing the user that the file is being generated in a download servlet

I have a download servlet which generates a ZIP with some files, one of them pretty big, and then sends the generated file in the response for download.
The problem is that the generating process is pretty big, and between the step of the generation of de ZIP and the download step (when the user see the download dialog) many seconds or even a minute could pass. So I would like to inform the user by anyway that the file is being generated.
The solution I'm thinking about is doing several requests, one for open a modal window which informs the user and inside this, then do another request automatically to an action which generates the file in a temp location and then, when the request ends, another one which closes the window (the window must be closed automatically) and then request to the download servlet the generated file in the last step.
If somebody understands what I'm trying to do, I would like if exists a better and cleaner solution.
You can fire an ajax request which will start the generation process, and then other ajax requests to poll the server if the file is ready. If yes - change the location of the browser to the file. Otherwise, show a "loading" message/image/..

Categories

Resources