javascript的ajax实例
请求的页面:
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>ajax测试01</title>
<script lanauge='javascript'>
var xmlrequest;
xmlrequest=false;
function InitObject()
{
if(!xmlrequest)
{
xmlrequest=new ActiveXObject("Msxml2.XMLHTTP");//IE6.0的
if(!xmlrequest)
{
xmlrequest=new ActionXObject("Microsof.XMLHTTP");//其他的IE
//else
// xmlrequest=new XMLHttpRequest();//不是IE的
}
}
}
var url;
function CallServer()
{
if(!xmlrequest)
InitObject();
if(xmlrequest)
{
url="GetTime.jsp";
xmlrequest.open("GET",url,true);
xmlrequest.onreadystatechange=UpdatePage;
xmlrequest.send(null);
}
}
function UpdatePage()
{
if(xmlrequest.readyState==4) //准备好了
{
var response=xmlrequest.responseText;
document.getElementById("labeltime").value=response;
//document.write(response);
}
}
function RefreshTime()
{
CallServer();
}
function init()
{
CallServer();
}
</script>
</head>
<body onload="javascript:init()">
<form name="frm1">
<input type="text" name="labeltime" size=55>
<input type="button" name="refresh" value="刷新" onclick="RefreshTime()">
</form>
</body>
</html>
<html>
<head>
<title>ajax测试01</title>
<script lanauge='javascript'>
var xmlrequest;
xmlrequest=false;
function InitObject()
{
if(!xmlrequest)
{
xmlrequest=new ActiveXObject("Msxml2.XMLHTTP");//IE6.0的
if(!xmlrequest)
{
xmlrequest=new ActionXObject("Microsof.XMLHTTP");//其他的IE
//else
// xmlrequest=new XMLHttpRequest();//不是IE的
}
}
}
var url;
function CallServer()
{
if(!xmlrequest)
InitObject();
if(xmlrequest)
{
url="GetTime.jsp";
xmlrequest.open("GET",url,true);
xmlrequest.onreadystatechange=UpdatePage;
xmlrequest.send(null);
}
}
function UpdatePage()
{
if(xmlrequest.readyState==4) //准备好了
{
var response=xmlrequest.responseText;
document.getElementById("labeltime").value=response;
//document.write(response);
}
}
function RefreshTime()
{
CallServer();
}
function init()
{
CallServer();
}
</script>
</head>
<body onload="javascript:init()">
<form name="frm1">
<input type="text" name="labeltime" size=55>
<input type="button" name="refresh" value="刷新" onclick="RefreshTime()">
</form>
</body>
</html>
响应的页面:
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%
java.util.Date date=new java.util.Date();
out.println("现在时间是:"+date.toString());
%>
<%
java.util.Date date=new java.util.Date();
out.println("现在时间是:"+date.toString());
%>