Get your CodeRanch badge!*
The moose likes Servlets and the fly likes response.setHeader( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "response.setHeader("Refresh", "10; URL=http:\\ www.javaranch.com");" Watch "response.setHeader("Refresh", "10; URL=http:\\ www.javaranch.com");" New topic
Author

response.setHeader("Refresh", "10; URL=http:\\ www.javaranch.com");

Anupreet Arora
Ranch Hand

Joined: Jun 17, 2003
Posts: 81
Hi,

A basic question about the Refresh header in response.

I read in a book that a common technique to avoid response from timing out for request that take a long time to process is is to set the

response.setHeader("Refresh", "10; URL=http:\\www.javaranch.com");

So my understanding of this is, if my response takes longer than 10 secs, the client will be taken to javaranch after 10 secs.
And when my final response does get generated, it will be sent to the client.

To test this out, I wrote a small servlet which just waits for 1 minute before sending a sample response to the client. I also set the Refresh header in the response. But I don't see move to javaranch.com after 10 secs. I just get my desired response after 1 minute

So, it'll be great if anybody could suggest what is wrong with my understanding or my code !!


Jyothi Lature
Ranch Hand

Joined: Oct 31, 2001
Posts: 60
Hello Anupreet,

This is nothing but Automatic Refreshing what you are trying to do in your servlet code, which is supported by many newer versions of Netscape Navigator and Microsoft Internet Explorer. This can also be done directly in the browser by using the <META> tag within the <HEAD> section of the document.
"Auto refreshing" means that once one page loads, you can set a certain number of seconds and then the browser will load another page automatically. The basic structure is as follows:
<meta http-equiv=REFRESH CONTENT="x_seconds; url = http://www.yourhost.com/pagetosendto.html">

You can also specify this via setHeader("Refresh", "x_seconds; url = http://www.yourhost.com/pagetosendto.html") in servlets.
It means "reload this page or go to the specified URL in x_seconds seconds." It does not mean "reload this page or go to the specified URL every x_seconds seconds."

In your case, since the servlet hasn't generated/loaded the HTML page completely, the www.javaranch.com site is not launched and when once the page is generated after waiting for 1 minute (which you have specified in your code), you have to wait for another 10secs to load this site after the page gets loaded.

Hope this explanation is clear...


Jyothi<br />Sun Certified Java Programmer<br />Brainbench Certified for Javascript
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Originally posted by Anupreet Arora:

Style note: don't go catching Throwables - there be dragons! You'll probably get a stack trace from your servlet container if things go bang in here during development. I would recommend only catching checked exceptions that aren't thrown by the doGet method. By catching Throwable you're catching Error as well as Exception which is something you would need to have a very good reason to do.

Jules
Anupreet Arora
Ranch Hand

Joined: Jun 17, 2003
Posts: 81
Hi,
Thanks for the wonderful explanation.. and by and large my understanding of the issue seems to be in sync with what your explanation is.

But I was reading this book by Faulkner, and came across this passage

Another response header technique that is uncommon but helpful is to send a wait page or a page that will auto-refresh to a new page after a given period of time. This tactic is helpful in any case where a response might take an uncontrollable time to generate, or for cases where you want to ensure a brief pause in a response. The entire mechanism revolves around setting the Refresh response header25. The header can be set using the following:

response.setHeader("Refresh", "time; URL=url" );

Where "time" is replaced with the amount of seconds, the page should wait, and "url" is replaced with the URL that the page should eventually load.


What is not clear to me in this is how is this useful in cases where a response might take an uncontrollable time to generate? In context of my example above, I don't want to go to javaranch.com after I have waited for 1 minute and another 10 secs. I want to go to javaranch after 10 secs while my response is taking one minute (read an uncontrollable time) to generate.

Appreciate your help ..
manuela marginean
Greenhorn

Joined: Mar 05, 2009
Posts: 7
I need to refresh my jsp after sending a file for download.
For this I've used this:

response.setHeader("Refresh", "time; URL=url" );

In mozilla is working perfectly but in ie it doesn't do a thing. Does anyone know anything about this problem?
Thank you in advance.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

If you have done web designing then you may find that most of the time,
it happend things working on mozilla doesnot works on IE,As here also we are setting the header which is bascially identified by the browser which only display this,
So may be the syntax which you are usiing is having some error which mozialla ignores but IE detects that so not showing your page.

So please check the syntax properly..

SCJP6.0,My blog Ranchers from Delhi
manuela marginean
Greenhorn

Joined: Mar 05, 2009
Posts: 7
Shanky Sohar wrote:If you have done web designing then you may find that most of the time,
it happend things working on mozilla doesnot works on IE,As here also we are setting the header which is bascially identified by the browser which only display this,
So may be the syntax which you are usiing is having some error which mozialla ignores but IE detects that so not showing your page.

So please check the syntax properly..


Thank you for your quick response.
Yes, I know that ie mostly doesn't work, but I really need to solve this problem.
Regarding my syntax, where could have I done wrong in this simple code:




This is the latest version, cause I've tried them all.

I forgot to mention that this works fine if I don't send a file for download from servlet to jsp.
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
Anupreet Arora wrote:Hi,

What is not clear to me in this is how is this useful in cases where a response might take an uncontrollable time to generate? In context of my example above, I don't want to go to javaranch.com after I have waited for 1 minute and another 10 secs. I want to go to javaranch after 10 secs while my response is taking one minute (read an uncontrollable time) to generate.

Appreciate your help ..


The way I have seen this mechanism used in practice most of the time is a scenario where 3 "pages" are involved. The 1st page - your input form. 2nd page - a "please wait while we process your request" page. 3rd page - the response "we're done, here's you answer" page. The input form actually submits not to the intended processing target action/servlet but to the wait page passing along all user entered data. The wait page usually has some animated equivalent of a spinning hourglass or progress meter of some kind. As soon as the wait page loads, it automatically uses the "Refresh" mechanism with a wait period of about 1 second to invoke the actual request processing servlet, again passing along all the user entered data from the first input page. Now the user continually sees the "wait" page whilst the long running request is processing on the backend. When the servlet is done it sends the 3rd page as the response. In essence the wait page is nothing but an intermdiary proxy to the actual form processing servlet so that the user gets the visual cue that something is happening on the backend and they are not as likely to want to hit refresh or something.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: response.setHeader("Refresh", "10; URL=http:\\ www.javaranch.com");
 
Similar Threads
problem with setContentType() method
Problem with redirect
IE 6 and GZIP
Refresh a Servlet page
'please wait' message while jsp is loading