標籤:img import image second images 類比 master ima port
以下內容引用自http://wiki.jikexueyuan.com/project/jsp/auto-refresh.html:
細想一個顯示線上比賽分數、股市狀態或當前交易額的網頁。對於所有這種類型的網頁,需要通過瀏覽器中的更新或者重新載入按鈕週期性重新整理網頁。
通過提供一個在給定的間距後自動重新整理網頁的機制,可以使JSP更加容易運行。
重新整理網頁最簡單的方法就是使用Request對象的setIntHeader()方法。下面是這個方法的符號描述:
public void setIntHeader(String header, int headerValue)
此方法將標題“重新整理”以及以秒為單位的時間間隔的整數值返回給瀏覽器。
自動頁面重新整理例子
下面的例子使用setIntHeader()方法設定Refresh標題來類比數字計時器:
<%@ page import="java.io.*,java.util.*" %><html><head><title>Auto Refresh Header Example</title></head><body><center><h2>Auto Refresh Header Example</h2><% // Set refresh, autoload time as 5 seconds response.setIntHeader("Refresh", 5); // 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; out.println("Crrent Time: " + CT + "\n");%></center></body></html>
現在將上面的代碼放在main.jsp中,並且訪問它。它將在之後每隔5s顯示當前系統時間。只運行此JSP頁面,等待一會可以看到如下結果:
測試工程:https://github.com/easonjim/5_java_example/tree/master/jspbasics/test15
JSP中自動重新整理