This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Servlets and the fly likes How to reload a page after commiting the response with an outputstream ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "How to reload a page after commiting the response with an outputstream ?" Watch "How to reload a page after commiting the response with an outputstream ?" New topic
Author

How to reload a page after commiting the response with an outputstream ?

Leinad Jan
Greenhorn

Joined: Feb 18, 2009
Posts: 28
Hi everybody,

I'm trying to automatically reload the page that is generating a file. Is there a way to redirect it after closing the outputstream ?
Venkat Sadasivam
Ranch Hand

Joined: May 10, 2008
Posts: 139
You can use html meta tag in your response to redirect page.
<META http-equiv="refresh" content="5;URL=http://www.google.com">


“Any fool can write code that a computer can understand. Good programmers write code that humans can understand. ”<br>
-Martin Fowler
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Leinad Jan wrote:Hi everybody,

I'm trying to automatically reload the page that is generating a file. Is there a way to redirect it after closing the outputstream ?

Wait .. You're writing a file to the outputstream using JSP?? You shouldn't be doing this. Use a Servlet. Otherwise the file will be cluttered with any template text which you wrote in JSP.

Go back to the link or button which you use to download the file. Let it invoke two requests asynchronously. One to download the file and another to change the location of the current page. You can use the actual action of the link or button for the one and some piece of Javascript in the onclick attribute for the other.

E.g.


Code depot of a Java EE / JSF developer | JSF / Eclipse / Tomcat kickoff tutorial | DAO kickoff tutorial | I ♥ Unicode
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Venkat Sadasivam wrote:You can use html meta tag in your response to redirect page.
<META http-equiv="refresh" content="5;URL=http://www.google.com">

Utterly bad idea. This string will be written to the file which he is trying to stream using the JSP page or it will even generate an IllegalStateException. The response should contain nothing else than the file. As said before, you don't achieve that with JSP, but with a Servlet.
Leinad Jan
Greenhorn

Joined: Feb 18, 2009
Posts: 28
Bauke Scholtz wrote:
Leinad Jan wrote:Hi everybody,

I'm trying to automatically reload the page that is generating a file. Is there a way to redirect it after closing the outputstream ?

Wait .. You're writing a file to the outputstream using JSP?? You shouldn't be doing this. Use a Servlet. Otherwise the file will be cluttered with any template text which you wrote in JSP.

Go back to the link or button which you use to download the file. Let it invoke two requests asynchronously. One to download the file and another to change the location of the current page. You can use the actual action of the link or button for the one and some piece of Javascript in the onclick attribute for the other.

E.g.


Hello,

no I don't use a JSP to write the CSV. It's done in an Action file. I don't need to create a servlet only for that either. I think your solution is what I'll do, asynchronous request. I'll put a timer that will call a new request, but with different parameters.

Thank you all for you help !
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
That's okay then. Only the part "the page that is generating the file" didn't sound very good.

Basically you should invoke two requests. One to download the file and one to navigate. You cannot send two responses back for one request. You also cannot send a response without a request.
Leinad Jan
Greenhorn

Joined: Feb 18, 2009
Posts: 28
Bauke Scholtz wrote:That's okay then. Only the part "the page that is generating the file" didn't sound very good.

Basically you should invoke two requests. One to download the file and one to navigate. You cannot send two responses back for one request. You also cannot send a response without a request.


The solution works well on my local server, but it doesn't work at all on an external server. Maybe I should remove the confirm in javascript that ask to the user if the file has been finished. Because when the external server is slower then the delay, the confirm window appears before the the download dialog. And it only appears when the confirm is gone. And the name of the file is weird...
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to reload a page after commiting the response with an outputstream ?
 
Similar Threads
Applet with Theme does not refresh properly
Thinking in 3D
To load the data for the entire application
Avoide double submit
Refresh problem in Servlets !!