简体   繁体   中英

Notification when servlet response received

I am creating reports (with JasperReports ) in a Java based web application. Each report is generated by a Servlet and is streamed back to the user. I have problems with reports that are sent back as attachments. (Excel reports must be sent back this way because browsers can not open Excel files). Moreover, these Excel reports take too much time to generate, therefore I open them in a new tab. Meanwhile I post an info message in the original tab. (something like "Please do not close the new tab").

The main problem is that I need to know when a report failed or succeeded. More specifically I need to know if response has been sent back and I need to know whether that response is a report or an error (right now I am sending back two different cookies). If it failed I need to show an error message, otherwise I need to remove the info message.

I have tried to use jQuery.post() to call the servlets but then then the download-dialog does not appear. Therefore I use form s. The server sends back a cookie no matter what. On client side I am waiting for cookies and when I receive one I decide what to do. This approach works BEAUTIFULLY as desicribed in a great post here . But what approach can I take if cookies are disabled on the client side?

Well, if somebody is interested in this question, here is how I finally managed to solve it. Instead of using a form to send my request to the Servlet I use the window.open() function. I open it in a new tab. Then I set an interval to check two things.

  • if the new window is closed ( newWindow.closed ) then I know the report generated successfully
  • if the newWindow.document.readyState is complete it means the report failed (because if the report fails I send back an inline response).

One advantage (in my case) of this approach (beyond that is works with cookies disabled) is that I can close the new window when an exception occurred.

NOTE: In IE8 after the window is closed the newWindow.document becomes unreachable (Permission denied). To check this I use typeof newWindow.document != 'unknown' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM