#2
JonnyKan2006-07-18 21:03
这些代码是JSP文件中的。
<%@ page language="java" contentType="text/html;charset=gb2312"%> <html> <head> <title>查看教师</title> <SCRIPT type="text/javascript"> var xmlHttp; //创建XMLHttpRequest对象 function createXMLHttpRequest() { if(window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else if(window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } } //向服务器发送请求 function startXMLHttpRequest(url) { alert(url); createXMLHttpRequest(); xmlHttp.onreadystatechange = doXMLHttpRequest; xmlHttp.open("GET",url,true); xmlHttp.send(null); } //响应请求 function doXMLHttpRequest() { if(xmlHttp.readyState == 4) { if(xmlHttp.status == 200) { alert("a"); clearPreviousResults(); parseResults(); } } } //清除 function clearPreviousResults() { var tableBody = document.getElementById("tResult"); while(tableBody.childNodes.length>0) { tableBody.removeChild(tableBody.childNodes[0]); } alert("b"); } //响应 function parseResults() { var result = null; var teaName = ""; var teaPass = ""; var teaRealName = ""; var teaDiscription = ""; alert("c"); var messages = xmlHttp.responseXML.getElementsByTagName("message"); var len = messages.length; for(var i = 0;i<len;i++) { alert("d"); result = messages[i]; teaName = result.getElementsByTagName("teaName")[0].firstChild.nodeValue; teaPass = result.getElementsByTagName("teaPass")[0].firstChild.nodeValue; teaRealName = result.getElementsByTagName("teaRealName")[0].firstChild.nodeValue; teaDiscription = result.getElementsByTagName("teaDiscription")[0].firstChild.nodeValue; addTableRow(teaName,teaPass,teaRealName,teaDiscription); } } //动态建立表格 function addTableRow(teaName,teaPass,teaRealName,teaDiscription) { var row = document.createElement("tr"); var cell = createCellWithText(teaName); row.appendChild(cell); cell = createCellWithText(teaPass); row.appendChild(cell); cell = createCellWithText(teaRealName); row.appendChild(cell); cell = createCellWithText(teaDiscription); row.appendChild(cell); document.getElementById("tResultBody").appendChild(row); } //建立列 function createCellWithText(text) { var cell = document.createElement("td"); var textNode = documnet.createTextNode(text); cell.appendChild(textNode); return cell; } </SCRIPT> </head> <body> <FORM action="#" name="frm1" onsubmit="startXMLHttpRequest('/servlet/teaServlet')"> <INPUT type="submit" name="btn1" value="查看所有教师"> </FORM> <!-- <FORM action="seeTeacher.jsp" name="frm2"> <INPUT type="text" name="tname"> <INPUT type="submit" name="btn2" value="查看"> </FORM>--> <SPAN>所有教师信息:</SPAN> <BR> <TABLE id="tResult"> <TBODY id="tResultBody"> </TBODY> </TABLE> </body> </html> |
最近在研究AJAX,照着书根据自己需求写些代码,可就是运行不出想要的结果,这里是我写的一个简单的程序,不知道为什么达不到目的,请高人帮忙看看。就两个文件,一个jsp,一个servlet,我先把代码复制上去,再说我的疑问。谢谢了!