Servlet automatically refreshes the page


Release date:2023-12-15 Update date:2023-12-15 Editor:admin View counts:110

Label:

Servlet automatically refreshes the page

Suppose there is a web page that shows on-the-spot competition results or stock market conditions or currency exchange rates. For all of these types of pages, you need to refresh the page regularly.

Java Servlet provides a mechanism for web pages to be automatically refreshed at given intervals.

The easiest 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 message “Refresh” back to the browser along with an integer value (in seconds) that represents the time interval.

Automatically refresh the page instance

This example demonstrates how to use Servlet setIntHeader() method to set the Refresh header information to automatically refresh the page.

package com.runoob.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 auto load event 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 Servlets to Automatically Refresh Pages";
        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’s compile the Servlet above, and in the web.xml create the following entries in the file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <servlet>
    <servlet-name>Refresh</servlet-name>
    <servlet-class>com.runoob.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 accessing http://localhost:8080/TomcatTest/Refresh.This will display the current system time every 5 seconds. Run the Servlet and wait for the results to be viewed:

Using Servlets to Automatically Refresh Pages
The current time is 9:44:50 PM

Powered by TorCMS (https://github.com/bukun/TorCMS).