Servlet Auto Refresh Page

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 auto refresh at a given time interval.

The simplest way to refresh a webpage is to use the response object's methodsetIntHeader(). 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.

Auto Refresh Page Example

This example demonstrates how the Servlet usessetIntHeader()the method to setRefreshthe header information, thereby achieving auto refresh of the page.

package com.example.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 {

        // 设置刷新自动加载的事件间隔为 5 秒
        response.setIntHeader("Refresh", 5);
     
        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
     
        // 获取当前的时间
        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.example.test.Refresh</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Refresh</servlet-name>
    <url-pattern>/TomcatTest/Refresh</url-pattern>
  </servlet-mapping>
</web-app>

Now invoke 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 a Page

Current time is: 9:44:50 PM

Other Extensions