二、testajax.html
<html>
<head>
<title>AJAX.html</title>
</head>
<script type="text/javascript">
function getResult(){
var url = "/Tajax/AajaxServlet?action=send";
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}else if (window.ActiveXObject){
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("GET",url, true);
req.onreadystatechange = complete;
req.send(null);
}
}
/*分析返回的文本文档*/
function complete(){
if (req.readyState == 4) {
if (req.status == 200) {
// alert(req.responseText);
var strResult = unescape(req.responseText);
var arrResult = strResult.split("@@@");
RemoveRow(); //删除以前的数据.
num1 = arrResult[0]; //字段num1的值
num2 = arrResult[1]; //字段num2的值
num3 = arrResult[2];
num4 = arrResult[3];
row1= tb.insertRow();
cell1 = row1.insertCell();
cell1.innerText = num1;
cell2 = row1.insertCell();
cell2.innerText = num2;
row2= tb.insertRow();
cell3 = row2.insertCell();
cell3.innerText = num3;
cell4 = row2.insertCell();
cell4.innerText = num4;
} else {
alert('There was a problem with the request.');
}
}
}
function RemoveRow(){//保留第一行表头,其余数据均删除.
var iRows = tb.rows.length;
for(var i=0;i< iRows-1;i++){
tb.deleteRow(1);
}
}
function MyShow(){//2秒自动刷新一次,2秒取得一次数据.
timer = window.setInterval("getResult()",2000);
}
</script>
<body onload="MyShow()">
<table width="47%" height="23" border="0" cellpadding="1" cellspacing="0" id="tb">
<tr><td>代码</td>
<td>价格</td>
</tr>
</table>
<form>
<p>输入股票代码:
<input type="text" size="14" name="phone" id="phone"/>
</p>
<p>你交易的数量:</p>
<p><textarea name="order" rows="6" cols="50" id="order"></textarea></p>
<p><input type="submit" value="提交" id="submit" /></p>
</form>
</body>
三、web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>AajaxServlet</servlet-name>
<servlet-class>com.ajaxServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AajaxServlet</servlet-name>
<url-pattern>/AajaxServlet</url-pattern>
</servlet-mapping>
</web-app>
|