以下示例将使用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> <% //将刷新,自动加载时间设置为5秒 response.setIntHeader("Refresh", 5); //获取当前时间 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 + "\n"); %> </center> </body> </html>
现在将上面的代码放在main.jsp中,并尝试访问它。每隔5秒将显示当前系统时间,如下所示。运行JSP。您将收到以下输出:-
Current Time is: 9:44:50 PM