YouTip LogoYouTip

Servlet Auto Refresh

Servlet Auto-Refresh Page

Suppose there is a webpage that displays live match scores, stock market conditions, or currency exchange rates. For all these types of pages, you need to refresh the webpage periodically.

Java Servlet provides a mechanism that allows a webpage to automatically refresh at a given time interval.

The simplest way to refresh a webpage is to use the response object's method setIntHeader(). Here is the definition of this method:

public void setIntHeader(String header, int headerValue)

This method sends the header "Refresh" along with an integer value representing the time interval (in seconds) back to the browser.

This example demonstrates how a Servlet uses the setIntHeader() method to set the Refresh header, thereby implementing automatic page refresh.

package com.tutorial.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Refresh
 */
@WebServlet("/Refresh")
public class Refresh extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Set the refresh interval to 5 seconds
        response.setIntHeader("Refresh", 5);

        // Set response content type
        response.setContentType("text/html;charset=UTF-8");

        // Get the current time
        Calendar calendar = new GregorianCalendar();
        String am_pm;
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        if (calendar.get(Calendar.AM_PM) == 0)
            am_pm = "AM";
        else
            am_pm = "PM";

        String CT = hour + ":" + minute + ":" + second + " " + am_pm;

        PrintWriter out = response.getWriter();
        String title = "Auto Refresh Page using Servlet";
        String docType = "<!DOCTYPE html> n";
        out.println(docType + "<html>n" +
                    "<head><title>" + title + "</title></head>n" +
                    "<body bgcolor="#f0f0f0">n" +
                    "<h1 align="center">" + title + "</h1>n" +
                    "<p>Current Time is: " + CT + "</p>n");
    }
}

Now let us compile the above Servlet and create the following entry in the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <servlet>
    <servlet-name>Refresh</servlet-name>
    <servlet-class>com.tutorial.test.Refresh</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Refresh</servlet-name>
    <url-pattern>/TomcatTest/Refresh</url-pattern>
  </servlet-mapping>
</web-app>

Now call this Servlet by visiting http://localhost:8080/TomcatTest/Refresh. This will display the current system time every 5 seconds. Run the Servlet and wait to see the result:

Auto Refresh Page using Servlet

Current Time is: 9:44:50 PM

← Servlet Sending EmailServlet Hits Counter β†’