Refresh a Web Page Using In Servlet

In this section, you will learn how to refresh servlets page.

Refresh a Web Page Using In Servlet


In this simplified example we develop an application to Refresh a web Page using Servlet. We create two file timer.html and When a web page ("timer.html") run on browser then it will call to Servlet ("") and refresh this web page and print the current Date and Time after 10 sec on the browser as a output.

Step 1: Create a web page(timer.html) to call a Servlets.


  <TITLE>Refresh Servlet Timer</TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <style type="text/css">
A:link {text-decoration: none;
    padding: 3px 7px;
    margin-right: 3px;
    border-bottom: none;
    color: #2d2b2b;  }
A:visited {text-decoration: underline;
    padding: 3px 7px;
    margin-right: 3px;
    color: #2d2b2b; }
A:active {text-decoration: none}
A:hover {text-decoration: none;
    padding: 3px 7px;
    margin-right: 3px;
    border: 0px;

    color: #2d2b2b; }


 <br><br><br> <br><br><br>
 <table width="200px" height="100px" 
  align="center" bgcolor="#BBFFFF" border=0>
      <td style="text-align:top;" valign="middle" 
      align="center" border=0>
   <a href="timer" ><b>Refresh Servlet Timer</b></a>



Step 2:Create a Servlet ( which refresh the page after every 10 seconds. 

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class timer extends HttpServlet{ 
 public void doGet(HttpServletRequest request, 
  HttpServletResponse response)
  PrintWriter out = response.getWriter();
  Date now = new Date(); // The current date/time
  out.println("<head><title> Time Check </title></head>");
  ("<table  width='100%' align='center' valign='top'>");
  out.println("<td valign='top' align='center' valign='top'>");
   ("<p style='color:#00000;font-size:20pt'>"+
  "<b>The Time is Refresh After 10 Seconds.</b></p>");
  out.println("<td style='background-color:#C6EFF7;color:blue;'"+
  " width='50' align='center'>");
  out.println("<b>The current time is: " + now + "</b>");
  response.setHeader("Refresh", "10");

Save the above file into "timer\WEB-INF\classes" directory.

Step 3: Mapping the servlet ( in to web.xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns=""

<display-name>Welcome to Tomcat</display-name>
Welcome to Tomcat


Step 4: Now compile the java code using javac command from command prompt.

Step 5: Start tomcat and type http://localhost:8080/timer/timer.html in the browser and Click on Text Link "Refresh Servlet Timer" . Your browser should display the Current Time and Refresh after 10 seconds.  

Successful Output of the program:


Download the full web application shows here.

Download the application

Share on Google+Share on Google+

Refresh a Web Page Using In Servlet

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



Discuss: Refresh a Web Page Using In Servlet   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
January 31, 2012
Some mistakes observed

The table tag was not closed and the "td" tag does not have "border" attribute.
July 2, 2012
how to call html from servlet

I have a html page containing meta tag with its url to a servlet displaying an image.Now I need to call that html page into another servlet which should display some text from database along with that first described image. please give some example code.