Với một trang web chỉ hiển thị số chứng khoán, tỷ giá hối đoái, … thì bạn cần Làm mới các trang này bằng cách sử dụng nút Làm mới hoặc Tải lại trên trình duyệt của bạn
JSP giúp công việc này được thực hiện dễ dàng hơn bằng việc cung cấp cho bạn một kỹ thuật giúp bạn Làm mới trang web một cách tự động sau một khoảng thời gian đã cho
Cách đơn giản nhất để làm mới trang web là sử dụng phương thức setIntHeader[] của phản hồi đối tượng
Sau đây là cú pháp cho phương thức này
public void setIntHeader[String header, int headerValue]
Phương thức này gửi tiêu đề trường là "Làm mới" trở lại trình duyệt cùng một giá trị số nguyên chỉ khoảng thời gian bằng giây
Ví dụ về Auto Refresh trong JSP
Sau đây là ví dụ minh họa cách một JSP thực hiện việc Tự động làm mới trang web bằng cách sử dụng phương thức setIntHeader[] để thiết lập Làm mới tiêu đề
Auto Refresh Header Example
Auto Refresh Header Example
Bây giờ set code on main. jsp and try to access it. Nó hiển thị thời gian hệ thống hiện tại sau mỗi 5 giây như sau. You run this JSP and queued results
Giả sử chúng tôi đã cung cấp một tài liệu HTML và nhiệm vụ là tự động làm mới trang web sau một khoảng thời gian nhất định trong trình duyệt web. Chúng tôi sẽ xác định trước một khoảng thời gian và trình duyệt sẽ tự động làm mới trang web
Thí dụ. Bạn đang tạo một trang web tự động làm mới cần được làm mới sau một khoảng thời gian ngắn hơn nhất định. Vì vậy, trong trường hợp này, bạn có thể sử dụng thẻ meta http-equiv để làm mới trang web. Một minh họa khác của thẻ http-equiv này là nó có thể được sử dụng để tải lại một trang web thời tiết cần được cập nhật sau mỗi khoảng thời gian nhỏ để hiển thị những thay đổi thời tiết từng phút
Cách tiếp cận 1. Người ta có thể tự động làm mới trang web bằng cách sử dụng thẻ meta trong phần đầu của HTML của bạn bằng cách sử dụng thuộc tính http-equiv. Nó là một thuộc tính sẵn có với HTML 5. Người ta có thể thêm khoảng thời gian làm mới bằng cách sử dụng thuộc tính nội dung trong thẻ Meta
Thuộc tính tương đương HTTP có thể được sử dụng để mô phỏng tiêu đề phản hồi HTTP. Thuộc tính được hỗ trợ bởi tất cả các trình duyệt web chính như Google Chrome, Mozilla Firefox, Microsoft Edge, Safari, Opera Mini, v.v.
cú pháp
Thí dụ
Auto Refresh Header Example
Auto Refresh Header Example
2
Auto Refresh Header Example
Auto Refresh Header Example
3____14
Auto Refresh Header Example
Auto Refresh Header Example
5
Auto Refresh Header Example
Auto Refresh Header Example
6
Auto Refresh Header Example
Auto Refresh Header Example
3____18____15function autoRefresh[] { window.location = window.location.href; } setInterval['autoRefresh[]', 5000];0
Auto Refresh Header Example
Auto Refresh Header Example
3function autoRefresh[] { window.location = window.location.href; } setInterval['autoRefresh[]', 5000];2
function autoRefresh[] { window.location = window.location.href; } setInterval['autoRefresh[]', 5000];3
function autoRefresh[] { window.location = window.location.href; } setInterval['autoRefresh[]', 5000];2
Auto Refresh Header Example
Auto Refresh Header Example
5function autoRefresh[] { window.location = window.location.href; } setInterval['autoRefresh[]', 5000];0______13
function autoRefresh[] { window.location = window.location.href; } setInterval['autoRefresh[]', 5000];8
function autoRefresh[] { window.location = window.location.href; } setInterval['autoRefresh[]', 5000];9
Auto Refresh Header Example
Auto Refresh Header Example
20
Auto Refresh Header Example
Auto Refresh Header Example
21
Auto Refresh Header Example
Auto Refresh Header Example
22
Auto Refresh Header Example
Auto Refresh Header Example
20
Auto Refresh Header Example
Auto Refresh Header Example
24
Auto Refresh Header Example
Auto Refresh Header Example
5
Auto Refresh Header Example
Auto Refresh Header Example
26
Auto Refresh Header Example
Auto Refresh Header Example
8
Auto Refresh Header Example
Auto Refresh Header Example
5
Auto Refresh Header Example
Auto Refresh Header Example
6
Auto Refresh Header Example
Auto Refresh Header Example
3____131
Auto Refresh Header Example
Auto Refresh Header Example
5function autoRefresh[] { window.location = window.location.href; } setInterval['autoRefresh[]', 5000];0______13
Auto Refresh Header Example
Auto Refresh Header Example
35
Auto Refresh Header Example
Auto Refresh Header Example
36
Auto Refresh Header Example
Auto Refresh Header Example
35
Auto Refresh Header Example
Auto Refresh Header Example
5function autoRefresh[] { window.location = window.location.href; } setInterval['autoRefresh[]', 5000];0______13
Auto Refresh Header Example
Auto Refresh Header Example
41
Auto Refresh Header Example
Auto Refresh Header Example
42
Auto Refresh Header Example
Auto Refresh Header Example
41
Auto Refresh Header Example
Auto Refresh Header Example
5
Auto Refresh Header Example
Auto Refresh Header Example
26
Auto Refresh Header Example
Auto Refresh Header Example
31____15
Auto Refresh Header Example
Auto Refresh Header Example
6
Auto Refresh Header Example
Auto Refresh Header Example
26
Auto Refresh Header Example
Auto Refresh Header Example
4
Auto Refresh Header Example
Auto Refresh Header Example
5đầu ra
Cách tiếp cận 2. Sử dụng phương thức setInterval[]. Một phương pháp khác để truy cập thuộc tính tự động làm mới của trang web là sử dụng mã JavaScript sau cho đến khi ClearInterval[] được gọi, setInterval[] sẽ tiếp tục tự gọi liên tục