问题描述
首先写了一个test.jsp代码如下:<%@pagelanguage="java"pageEncoding="GBK"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html;charset=GBK"/><title>仿百度搜索的下拉框效果</title><scripttype="text/javascript">functiontextsearch(){vartextName=document.getElementById("textname");vardataNode=document.getElementById("data");while(dataNode.hasChildNodes()){dataNode.removeChild(dataNode.firstChild);}if(textName.value!=""){listName(textName.value);}}functionreadyAJAX(){try{returnnewXMLHttpRequest();}catch(e){try{returnnewActiveXObject("Msxm12.XMLHTTP");alert("1");}catch(e){try{returnnewActiveXObject("Microsoft.XMLHTTP");alert("2");}catch(e){return"Anewerbrowserisneeded.";alert(3);}}}}functionlistName(text){varnameList=AJAXresponse.split(",");varpattern="^"+text;varnameRegexp=newRegExp(pattern,"i");for(vari=0;i<nameList.length;i++){varexistDiv=document.getElementById(nameList[i]);if(!existDiv){if(nameList[i].match(nameRegexp)){vardisplayDiv=document.getElementById("data");varnewDiv=document.createElement("div");if(window.attachEvent){newDiv.attachEvent("onclick",function(e){document.forms["nameform"].textname.value=e.srcElement.firstChild.nodeValue;});newDiv.attachEvent("onmouseover",function(e){e.srcElement.style.background="#FFFFFF";});newDiv.attachEvent("onmouseout",function(e){e.srcElement.style.background="#aaabba";});}else{newDiv.addEventListener("click",function(){document.forms["nameform"].textname.value=this.firstChild.nodeValue;},false);newDiv.addEventListener("mouseover",function(){this.style.background="#FFFFFF";},false);newDiv.addEventListener("mouseout",function(){this.style.background="#aaabba";},false);}newDiv.setAttribute("id",nameList[i]);newDiv.style.background="#aaabba";newDiv.style.color="#000000";newDiv.style.border="solid1px";newDiv.style.display="block";newDiv.style.width="175px";newDiv.appendChild(document.createTextNode(nameList[i]));displayDiv.appendChild(newDiv);}}}}varrequestObj=readyAJAX();varurl="LikeFinding?name="+document.getElementById("textname");requestObj.open("GET",url,true);requestObj.send();varAJAXresponse;requestObj.onreadystatechange=function(){if(requestObj.readyState==4){if(requestObj.status==200){AJAXresponse=requestObj.responseText;alert(AJAXresponse);}else{alert(requestObj.statusText);}}}if(window.attachEvent){document.getElementById("textname").attachEvent("onkeyup",textsearch);}else{document.getElementById("textname").addEventListener("keyup",textsearch,false);}</script></head><body><formname="nameform"id="nameform"action=""method="post">输入药码:<inputid="textname"type="text"name="textname"/></form><divid="data"></div></body></html>然后写了一个服务器应用程序,是相应请求的CLASS,代码如下:packagecom.servlet;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.Iterator;importjava.util.List;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.hibernate.DAO.Yaokuxinxi.Insert;importcom.hibernate.pojo.Yaokuxinxi;publicclassLikeFindingextendsHttpServlet{publicLikeFinding(){super();}publicvoiddestroy(){super.destroy();}publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{System.out.println("servletworking");response.setContentType("text/html");PrintWriterout=response.getWriter();Stringyaoming=request.getParameter("name");Stringresult="";Stringno=null;if(yaoming.equals("")||yaoming.length()<1){result="请填写姓名!";}else{Insertq;q=newInsert();try{Listl=q.queryByLike(yaoming);Iteratoriter=l.iterator();while(iter.hasNext()){Yaokuxinxispider=(Yaokuxinxi)iter.next();result+=spider.getYaoming()+",";no+=spider.getYaoma();//消除id,不让输出}}catch(Exceptione){e.printStackTrace();}}out.write(result);out.flush();out.close();}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType("text/html");response.setContentType("text/xml;charset=GBK");PrintWriterout=response.getWriter();out.flush();out.close();}publicvoidinit()throwsServletException{//Putyourcodehere}}可是打开JSP后,alert(requestObj.statusText);这句就运行了,页面提示:NOTFOUND这是什么问题?是因为没有接受到服务器应用程序返回的数据吗?请高手指教PS:web.xml的配置文件正常
解决方案
解决方案二:
初学,提示:requestObj.send();应该传递null值,避免浏览器不兼容问题例如Firefox你的提交方式为:POST而你是在doGet()中处理页面问题,没有在doPost()中调用,dopost()无任何值返回过去!试试看吧!仅此知道的提示了!
解决方案三:
LZ注意发帖给点分,要不然谁给你回答问题啊!