如何定期重新整理一個 JSP 頁面?


以下示例將使用 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("Current Time is: " + CT + "
");          %>       </center> </body> </html>

將以上程式碼放入 **main.jsp** 中並嘗試訪問它。它將每 5 秒顯示一次當前系統時間,如下所示。執行 JSP。您將收到以下輸出:-

自動重新整理標題示例

Current Time is: 9:44:50 PM

更新於: 30-Jul-2019

281次瀏覽

開啟您的 職業生涯

透過完成課程獲取認證

開始學習
廣告