各位前輩大神,客戶端部分我想我寫的雖然不成熟,但應該沒問題,服務器部分怎么響應,怎么以XML格式或其他格式響應客戶端,這我無從下手,求指點?。?!JSP中的JS代碼如下:if(dom)的位置不反應,它前面的alert(dom)是undefined。<script?type="text/javascript">
???? $(document).ready(function(){
???? $("input[name='usercode']").blur(function(){
???? var?xmlHttpRequest=false;
???? if(window.XMLHttpRequest){//返回為true說明是新版本IE或其他瀏覽器
???? xmlHttpRequest=new?XMLHttpRequest();
???? }else{//返回值為false時,說明是老板本IE
???? xmlHttpRequest=new?ActieXObject("Microsoft.XMLHTTP");
???? }
???? //初始化組件
???? var?url="<%=path%>/DoCheckServlet";alert("d")
???? xmlHttpRequest.open("post",url,true);
//設置回調函數
xmlHttpRequest.onreadystatechange=callback;
function?callback(){
if?(xmlHttpRequest.readyState?==?4
&&?xmlHttpRequest.status?==?200)?{alert(1);
var?dom?=?xmlHttpRequest.responseXML;//獲取服務器端返回的XML形式的文檔對象
alert(2);alert(dom);
if?(dom)?{
//獲取節(jié)點名為usercode的節(jié)點對象集合
var?userNodes=dom.getElementsByTagName("usercode");
if(yserNodes.length>0){
var?username=userNodes[0].firstChild.nodeValue;
$("input[name=usercode]").attr("value",username);
alert(2);
}
}
}
}
//發(fā)送請求
var?usercode=$("[name=usercode]").val();
xmlHttpRequest.send(usercode);
});
});
</script>下面是DoCheckServlet的代碼,未完成,我寫不下去了,跪求指點?。?!public?class?DoCheckServlet?extends?HttpServlet?{
public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)
throws?ServletException,?IOException?,Exception{
//?獲取頁面參數
String?userCode?=?request.getParameter("usercode");
String?passWord=null;
String?path?=?(String)?request.getAttribute("path");
//?調用業(yè)務邏輯接口,用戶登錄
IUserService?userService?=?new?UserServiceImpl();
//獲取數據庫中所有的用戶集合
List<Users>?userlist=userService.query(usercode);
//下面該怎么將這個集合以XML格式傳到JSP頁面中的AJAX
}
}
1 回答
已采納

tom的貓
TA貢獻65條經驗 獲得超35個贊
將userList轉成js可解析的格式,這里推薦使用json
PrintWriter?out?=?response.getWriter(); ?
out.write(jsonArray.toString());?
- 1 回答
- 0 關注
- 1700 瀏覽
添加回答
舉報
0/150
提交
取消