如何定期刷新一个 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
广告