jsp 异步加载 刷新时间
引用:http://hi.baidu.com/lynn_wong/blog/item/33882a11f7461ee8c3ce7909.html
即:根据时间仅刷新网页部分内容
下面是异步显示系统时间的例子
client.jsp
<%@pagecontentType="text/html"%>
<%@pagepageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script type="text/javascript">
function makeRequest(url) {
alert("1111");
//alert("my GOD!");
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("您的浏览器不支持当前操作,请使用 IE 5.0 以上版本!");
return false;
}
//定义页面调用的方法init,不是init();没有();
http_request.onreadystatechange = init;
http_request.open('GET', url, true);
//禁止IE缓存
http_request.setRequestHeader("If-Modified-Since","0");
//发送数据
http_request.send(null);
//每60秒刷新一次页面
setTimeout("makeRequest('"+url+"')", 1000);
}
function init() {
if (http_request.readyState == 4) {
if (http_request.status == 0 || http_request.status == 200) {
var result = http_request.responseText;
if(result==""){
result = "系统时间获取失败";
}
document.getElementById ("sysTimeShow").innerHTML="系统时间 :"+result;
} else {//http_request.status != 200
alert("请求失败!");
}
}
}
</script>
</head>
<body onload="makeRequest('getSystemTime.jsp')">
<div id="sysTimeShow"></div>
</body>
</html>
getSystemTime.jsp
<%@ 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,19);
out.write(result,0,result.length());
%>
**********************************失眠、忧郁、焦虑的分割线*******************************
下面是前台框架extjs中使用异步刷新的方法:
var task={run:function(){
//刷新的内容,如:更新textfield的value等 timearea.setValue(time);
},interval:1000};//1000代表一秒刷新一次
var taskRunner=new Ext.util.TaskRunner();
taskRunner.start(task);
});
*****************************************睡醒了的分割线************************************
其实如果在extjs框架下如果想动态显示一个document节点 那么可以动态创建一个,
把它作为item来添加即可。
并且用上述方法赋值。
例子如下:(该例子用于在工具栏中动态显示当前系统时间)
var barr = new Ext.Toolbar({
height : 30
});
var doc = document.createElement('time');
barr.addItem(doc);
var task={run:function(){
var now= new Date();
var year=now.getYear();
var month=now.getMonth()+1;
var day=now.getDate();
var hour=now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();
var time = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
//上面是在获取当前时间
doc.innerHTML(time);
},interval:1000};
var taskRunner=new Ext.util.TaskRunner();
taskRunner.start(task);
});