JSP中自動重新整理

來源:互聯網
上載者:User

標籤: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中自動重新整理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.