The main page (the page that requires refreshing) requires the system time to be refreshed every minute
<html>
<body onload="makeRequest("getSystemTime.jsp")"> //Create httpRequest object
<form name="dateForm">
<table>
<td colspan="2" align="center">
<div id="sysTimeShow"></div> //Display platform system time
</td>
</table></form>
</body>
</html>
getSystemTime.jsp //Get the current time of the system, which is the page that outputs the results
<%@ page language="java" %>
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.util.Date"%>
<%@ page import="java.sql.Timestamp"%>
<%@ page import="java.io.IOException"%>
<%
Date d = new Date();
Timestamp ts = new Timestamp(d.getTime());
String result = ts.toString().substring(0,16);
out.write(result,0,result.length());
%>
ajax.js
function makeRequest(url) {
http_request = false;
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType){
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) {
try{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
}
}
}
if (!http_request) {
alert("Your browser does not support the current operation, please use IE 5.0 or above!");
return false;
}
//Define the method init called by the page, not init(); no();
http_request.onreadystatechange = init;
http_request.open('GET', url, true);
//Disable IE caching
http_request.setRequestHeader("If-Modified-Since","0");
//Send data
http_request.send(null);
//Refresh the page every 60 seconds
setTimeout("makeRequest('"+url+"')", 60000);
}
function init() {
if (http_request.readyState == 4) {
if (http_request.status == 0 || http_request.status == 200) {
var result = http_request.responseText;
if(result==""){
result = "Failed to obtain system time";
}
document.getElementById ("sysTimeShow").innerHTML="Platform system time:"+result;
} else {//http_request.status != 200
alert("Request failed!");
}
}
}