Easy Tutorial
❮ Servlet Cookies Handling Servlet Packaging ❯

Servlet "Automatic Page Refresh"

Suppose there is a webpage that displays live game 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 the webpage to refresh automatically at a given time interval.

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

public void setIntHeader(String header, int headerValue)

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

Automatic Page Refresh Example

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

package com.tutorialpro.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, autoload time 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 = "Using Servlet to Automatically Refresh Page";
        String docType = "<!DOCTYPE html> \n";
        out.println(docType +
            "<html>\n" +
            "<head><title>" + title + "</title></head>\n"+
            "&lt;body bgcolor=\"#f0f0f0\">\n" +
            "&lt;h1 align=\"center\">" + title + "</h1>\n" +
            "<p>Current time is: " + CT + "</p>\n");
    }

}

Now let's compile the above Servlet and create the following entries in the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <servlet>
    <servlet-name>Refresh</servlet-name>
    <servlet-class>com.tutorialpro.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 results:

| Using Servlet to Auto-Refresh Page Current time is: 9:44:50 PM |

❮ Servlet Cookies Handling Servlet Packaging ❯