Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Lập Trình

Cách refresh trang JSP theo interval định kỳ

Ví dụ sau dùng phương thức setIntHeader() để set header Refresh mô phỏng đồng hồ điện tử:

<%@ 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 + "\n");
            %>
        </center>
    </body>
</html>

Đặt code trên vào main.jsp và truy cập. Sẽ hiển thị thời gian hệ thống hiện tại sau mỗi 5 giây:

Auto Refresh Header Example

Current Time is: 9:44:50 PM