JSP auto-refresh
Imagine, if you want to live game scores, or real-time status of the stock market, or the current foreign exchange rationing, how to achieve it? Obviously, to achieve this real-time capabilities, you have to regularly refresh the page.
JSP provides a mechanism to make the job easy, it can be timed automatically refresh the page.
Refresh a page The easiest way is to use the response object setIntHeader () method. Signature of this method are as follows:
public void setIntHeader(String header, int headerValue)
This method tells the browser to refresh after a given period of time, the time in seconds.
Page automatically refresh program example
This example uses the setIntHeader () method to set the refresh head, simulating a digital clock:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.*,java.util.*" %> <html> <head> <title>自动刷新实例</title> </head> <body> <h2>自动刷新实</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("当前时间为: " + CT + "\n"); %> </body> </html>
The above code is stored in main.jsp file access it. It will refresh the page every five seconds and get the current system time. Results are as follows:
自动刷新实 当前时间为: 6:5:36 PM
You can also write their own hands more complex points of the program.