Example :
import javax.servlet.*; import java.io.*; public class ClientAutoServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { HttpSession session = request.getSession(); Long times = (Long) session.getAttribute("times"); if (times == null) session.setAttribute("times", new Long(0)); long value = 1; if (times != null) value = (times.longValue()) + 1; response.addHeader("Refresh", "5"); response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter(); out.println("<html><head><title>Client Auto Refresh Example</title></head><body>"); out.println("The Counter below will automatically increase after 5 seconds<br/><br/>"); out.println("You've visited this page " + value + " times."); session.setAttribute("times", new Long(value)); out.println("</body></html>"); } }
The method response.addHeader("Refresh", "5") refreshes the servlet after every 5 seconds till the servlet gets destroy.