Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The servlet automatically refreshes the page


May 15, 2021 Servlet


Table of contents


The servlet automatically refreshes the page

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

Java Servlet provides a mechanism for web pages to refresh automatically at a given interval.

The easiest way to refresh a Web page is to use the response object method setIntHeader(). Here's how to define it:

public void setIntHeader(String header, int headerValue)

This method sends the header message "Refresh" back to the browser along with an integer value that represents the interval, in seconds.

The page instance is automatically refreshed

This example demonstrates how servlets can set up Refresh header information using the setIntHeader() method to automatically refresh pages.

// 导入必需的 java 库
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
 
// 扩展 HttpServlet 类
public class Refresh extends HttpServlet {
 
  // 处理 GET 方法请求的方法
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      // 设置刷新自动加载的事件间隔为 5 秒
      response.setIntHeader("Refresh", 5);
 
      // 设置响应内容类型
      response.setContentType("text/html");
 
      // 获取当前的时间
      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 public \"-//w3c//dtd html 4.0 " +       "transitional//en\">\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");
  }
  // 处理 POST 方法请求的方法
  public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
      throws ServletException, IOException {
     doGet(request, response);
  }
}

Now let's compile the servlet above and create the following entry .xml the web server file:

....
 <servlet>
     <servlet-name>Refresh</servlet-name>
     <servlet-class>Refresh</servlet-class>
 </servlet>
 
 <servlet-mapping>
     <servlet-name>Refresh</servlet-name>
     <url-pattern>/Refresh</url-pattern>
 </servlet-mapping>
....

Now call this servlet http://localhost:8080/Refresh the URL or by accessing the URL. T his will show the current system time every 5 seconds. Run the servlet and wait to see the results:

Use the servlet to automatically refresh the page

The current time is: 9:44:50 PM