English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Servlet Page Auto-Refresh

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

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

The simplest way to refresh a web page is to use the method of the response object setIntHeader(). The following is the definition of this method:

public void setIntHeader(String header, int headerValue)

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

Automatic Page Refresh Example

This example demonstrates how Servlet uses setIntHeader() method to set Refresh Header information, thus realizing automatic page refresh.

package com.w;3codebox.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 interval for automatic refresh to load events 5 seconds
        response.setIntHeader("Refresh", 5);
     
        // Set the 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 = "使用 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>当前时间是:" + CT + "</p>\n");
    }
}

Now let's 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.w3codebox.test.Refresh</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Refresh</servlet-name>
    <url-pattern>/TomcatTest/Refresh</url-pattern>
  </servlet-mapping>
</web-app>

Now, by accessing http://localhost:8080/TomcatTest/Refresh to call this Servlet. It will refresh every 5 Seconds display the current system time once. Run the Servlet and wait to see the results:

Use Servlet to Auto-Refresh Web Page

The current time is:9:44:50 PM