一个简单的ajax

一个简单的ajax

index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<script language="javascript">
function detect(){
  xml=new ActiveXObject("Microsoft.XMLHTTP");
  var post="";
  xml.open("POST","http://localhost:8080/xmlhttp.jsp",false);
  xml.setrequestheader("content-length" ,post.length);
  xml.setrequestheader("content-type" , "application/x-www-form-urlencoded" );
  xml.send(post);
  var res=xml.responseText;
  list.innerText=res;
  setTimeout("detect()",1000);
}
</script>

<body onload = "detect()">
test :
     <a id="list" >  </a>

</BODY>
</HTML>


xmlhttp.jsp
<%@ page language="java" contentType="text/html;charset=GB2312"%>
<%@ page import ="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<%
  Random randor=new Random();
  out.println(randor.nextInt());
%>
</BODY>
</HTML>

你可能感兴趣的:(一个简单的ajax)