Servlets 简明教程
Servlets - Auto Page Refresh
考虑一个显示现场比赛比分、股票市场状况或汇率的网页。对于所有这些类型的页面,您都需要使用浏览器的刷新或重新加载按钮定期刷新您的网页。
Consider a webpage which is displaying live game score or stock market status or currency exchange ration. For all such type of pages, you would need to refresh your web page regularly using refresh or reload button with your browser.
Java Servlet 通过为您提供一种机制来简化此项工作,您可以在这种机制中以这样的方式制作网页,使其在给定的时间间隔后自动刷新。
Java Servlet makes this job easy by providing you a mechanism where you can make a webpage in such a way that it would refresh automatically after a given interval.
刷新网页的最简单方法是使用响应对象的 setIntHeader() 方法。以下是该方法的签名 -
The simplest way of refreshing a web page is using method setIntHeader() of response object. Following is the signature of this method −
public void setIntHeader(String header, int headerValue)
此方法将标头 "Refresh" 连同表示时间间隔(以秒为单位)的整数值发送回浏览器。
This method sends back header "Refresh" to the browser along with an integer value which indicates time interval in seconds.
Auto Page Refresh Example
此示例演示 servlet 如何使用 setIntHeader() 方法来设置 Refresh 标头以执行自动页面刷新。
This example shows how a servlet performs auto page refresh using setIntHeader() method to set Refresh header.
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
// Extend HttpServlet class
public class Refresh extends HttpServlet {
// Method to handle GET method request.
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set refresh, autoload time as 5 seconds
response.setIntHeader("Refresh", 5);
// Set response content type
response.setContentType("text/html");
// Get 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 = "Auto Page Refresh using 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>Current Time is: " + CT + "</p>\n"
);
}
// Method to handle POST method request.
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
现在让我们编译上述 servlet,并在 web.xml 中创建以下项
Now let us compile the above servlet and create the following entries in web.xml
....
<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>
....
现在使用 URL [role="bare"] [role="bare"]http://localhost:8080/Refresh 调用此 servlet,它将在每 5 秒后显示当前系统时间,如下所示。只需运行 servlet 并等待查看结果 -
Now call this servlet using URL [role="bare"]http://localhost:8080/Refresh which would display current system time after every 5 seconds as follows. Just run the servlet and wait to see the result −
Auto Page Refresh using Servlet
Current Time is: 9:44:50 PM