Share on Google+Share on Google+

Client Auto Refresh in Servlets

This section illustrates you how client gets auto refresh.

Client Auto Refresh in Servlets


This section illustrates you how client gets auto refresh.

We are providing you an example which explains you clearly. In the example, We have created the session by request.getSession() method. The method response.addHeader("Refresh", "15") refreshes the servlet after every 15 seconds till the servlet gets destroy.

Here is the code of

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
  public class ClientAutoServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, 
throws ServletException, {
  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;
  if (value < 6)
  response.setContentType("text/html"); out = response.getWriter();
"<html><head><title>Client Auto Refresh Example
"You've visited this page " + value + " times.");
  session.setAttribute("times"new Long(value));

In web.xml, do the servlet-mapping



Output will be displayed as:

After 15 seconds, page will get refresh. Output will be:

Download Source Code


Posted on: August 20, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Client Auto Refresh in Servlets   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image