2.19. Servlet automatically refreshes the page

发布时间 : 2025-10-25 13:33:40 UTC      

Page Views: 9 views

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.

2.19.1. 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 = " \n"; out.println(docType + "\n" + ""</span> <span class="o">+</span> <span class="n">title</span> <span class="o">+</span> <span class="s2">"\n"+ "\"#f0f0f0\">\n" + "

\"center\">" + title + "\n" + "

current time is:" + CT + "\n"); } }

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

   Refresh com.runoob.test.Refresh   Refresh /TomcatTest/Refresh   

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 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。