File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to refresh a Servlet automatically ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to refresh a Servlet automatically ?" Watch "How to refresh a Servlet automatically ?" New topic
Author

How to refresh a Servlet automatically ?

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello,
I would be highly obliged if someone can guide me in refreshing the Servlet page, say after n seconds .
I think we can use atleast the following two options , i.e :
1. Using the <META HTTP-EQUIV="Refresh" CONTENT="5" URL=http://host/path"> and
2. Using setHeader("Refresh","10") method of HttpServletResponse.
However , in the first case , I get a message saying that "the page cannot be refreshed without sending the information..." and
then if I press the Retry button it does refresh the servlet . But this defeats the very aim of AUTOMATIC refresh after , say 5 secs.
In the 2 option , I do not know how to use the Refresh header at the Request time . I mean how to use this header , as I have not worked on many headers other than cookies.
I also read somewhere( but did not really understand) about the Pragma header , is it of any use for this question.
Thanks in Anticipation,
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
1. You can use it without the URL, it will refresh the same servlet.
2. You can put res.setHeader("refresh", "10") anywhere in the doGet or doPost. However, this method will keep the connection on which is a waste of resources.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks "the sheik " for your reply.
But I still need more help.
As stated by you I know we can use it , but HOW ?
Merely by writting response.setHeader("refresh","10") will set the header but then how to proceed , i.e how to retrieve the header .. etc.
Even in the first option ( Meta tag ),I have generated the Meta tag through PrintWriter. However it does NOT refresh Automatically .
I would be highly obliged if u can spare some time and help me .
Thanks a lot.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I assume that your servlet code worked and you only need to refresh it. Can you post the part of the code around the <META-HTTP tags?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks for your valued response.
Yes my servlet is working perfectly.
I am trying to make an online Test Center, wherein if the user does not answer a particular question it should automatically produce another question after 60 secs.
I have tried two things :
a) Using the meta tags. The code is as follows :

pw.println("<meta http-equiv=refresh content=30 url=http://localhost:8080/servlet/exam>");
pw.println("<body bgcolor=#123abc text=white>");
pw.println("<form name=ans onsubmit=check1(this) action=http://localhost:8080/servlet/exam method=get>");

{I am not providing other codes like JavaScript which is not rtequired for our debug }
The above code generates the message " This page cannot be refreshed without resending...."
b) Than I tried this way :
public void doPost(HttpServletRequest req, HttpServletResponse res)
{
res.setHeader("refresh","5");
res.setHeader("Cache-control","no-cache");
ses1=req.getSession(true);
:
:
This also does not seem to work!
Also res.setHeader("Cache-control","no-cache") which should have ensured that the browser back button is disabled is also not working .
Kindly try and throw some light on this .
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks for your valued response.
Yes my servlet is working perfectly except for these things .
I am trying to make an online Test Center, wherein if the user does not answer a particular question it should automatically produce another question after 60 secs.
I have tried two things :
a) Using the meta tags. The code is as follows :

pw.println("<meta http-equiv=refresh content=60 url=http://localhost:8080/servlet/exam>");
pw.println("<body bgcolor=#123abc text=white>");
pw.println("<form name=ans onsubmit=check1(this) action=http://localhost:8080/servlet/exam method=get>");

{I am not providing other codes like JavaScript which is not rtequired for our debug }
The above code generates the message " This page cannot be refreshed without resending...."
b) Than I tried this way :
public void doPost(HttpServletRequest req, HttpServletResponse res)
{
res.setHeader("refresh","60");
res.setHeader("Cache-control","no-cache");
ses1=req.getSession(true);
:
:
This also does not seem to work!
Also res.setHeader("Cache-control","no-cache") which should have ensured that the browser back button is disabled is also not working .
Kindly try and throw some light on this .

[This message has been edited by Frank Carver (edited August 23, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to refresh a Servlet automatically ?