Forums Register Login
auto servlet refresh
This question might have been asked some time back in this very forum if so kindly redirect me.
How can i 'Refresh' a servlet automatically every 5 seconds.

Best regards
Bharath Chinnadurai Maharajan
SCJP 1.4
You would need to initiate this from the client side using either Javascript or the HTML META tag.
isnt there a java way of doing this instead of java script.
I would like to do it in java or Jsp.

Best Regards
Bharath Chinnadurai Maharajan
SCJP 1.4
A servlet sits and listens for a request and then sends out a response.
Once this request/response cycle has ended, the servlet has no way to connect back to a browser. Thus the term stateless.

If you want to refresh a page, you have to issue a new request. Unless you're running applets, there is no way to do this with Java.
You can send a refresh header, from the servlet. I don't know how widely supported the refresh header is. But, give it a try and let us know!


The 60 is the number of seconds to wait and the URL is the page.
[ August 15, 2005: Message edited by: Kerry Wilson ]
Doh! <slap> Good catch Kerry. Yes, setting the header as such is equivalent to using the HTML META mechansim.
thx friends,
I will give a try and get back to you
bharath chinnadurai maharajan
scjp 1.4

Originally posted by Bear Bibeault:
Doh! <slap> Good catch Kerry. Yes, setting the header as such is equivalent to using the HTML META mechansim.

NOTE: This is still "The client making a new request" and not, as the original poster put it an "auto refresh servlet". Nitpicking?, yes but not understanding the whole request/response nature of web programming often leads to a whole host of other mis-conceptions.
kerry, That worked

This is still "The client making a new request"

Iam a beginner for servlets world, so correct me if iam wrong.
How can this be like a client making a new request. In this method
response.setIntHeader("Refresh", 60) the server reloads the content every 60 seconds. I dont understand where does a client is making a request here.

Bharath Chinnadurai Maharajan
SCJP 1.4
[ August 15, 2005: Message edited by: Bharath Chinnadurai Maharajan ]
That header goes down to the browser with the response.
It is a suggestion that the browser make a request for that URL in n seconds.

All a servlet can do is respond to a request. Once it does, the connection is closed and the servlet is completely unaware of any particular client (browser) until it makes another request.

A great tool for seeing this in action is the LiveHTTPHeaders plugin for Mozilla/Firefox. With it, you can see both the request headers coming from the browser and the response headers coming from the server in real time as they are being made.
[ August 15, 2005: Message edited by: Ben Souther ]
hey Ben, Now i understood the logic behind this, also the Live Http Headers is a cool one. Guess people like me who are starting servlets could use tools like this for better understanding.

And Ben iam asking just out of curiosity,
I developed a servlet which displays updated time every 2 sec.
I came across in the Live-Http-Header tool some thing like this

It loops on until i close the servlet.
My doubt is what the icon file (favicon.ico) all about. Iam using tomcat 4.1. Again i would like to mention "Just out of curiosity".

Bharath Chinnadurai Maharajan
SCJP 1.4
Ben's point is an important one. Especially in light of the fact that the client can ignore headers at its discretion.

It is the client initiating the new request at the suggestion provided by the servlet in the guise of the header. The servlet engine itself is completely univolved in this activity.

The same goes for headers that suggest that the browser not do any caching.
[ August 15, 2005: Message edited by: Bear Bibeault ]
With regards to favicon.ico, it's best to open a new topic for new questions rather than tacking them onto the end of an unrelated thread.
Hey! Wanna see my flashlight? It looks like this tiny ad:
Why should you try IntelliJ IDEA ?

This thread has been viewed 6690 times.

All times above are in ranch (not your local) time.
The current ranch time is
Aug 16, 2018 14:35:19.