a516784919
2009-09-22 02:10
浏览 313
已采纳

为什么AJAX调用不了Servlet控制器

1、这是登录页面login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<br> var XMLHttpReq = false;<br> function creatXMLHttpRequest(){<br> if (window.ActiveXObject) {<br> xmlhttp = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);<br> } else {<br> if (window.XMLHttpRequest) {<br> xmlhttp = new XMLHttpRequest();<br> }<br> }<br> }<br> //发送请求的函数<br> function sendRequest(url){<br> alert(url);<br> creatXMLHttpRequest();<br> XMLHttpReq.open(&quot;GET&quot;,url,true);<br> XMLHttpReq.onreadystatechange = processResponse();//指定响应函数<br> alert(&quot;66&quot;);<br> XMLHttpReq.send(null);//发送请求<br> }<br> //处理返回信息函数<br> function processResponse(){<br> alert(&quot;3333&quot;);<br> if(XMLHttpReq.readyState==4)//判断对象状态<br> {<br> alert(&quot;444&quot;);<br> if(XMLHttpReq.status==200){//信息已经成功返回,开始处理信息<br> alert(&quot;55555&quot;);<br> var res = XMLHttpReq.responseXML.getElementsByTagName(&quot;res&quot;)[0].firstChild.data;<br> window.alert(res);<br> }else{<br> window.alert(&quot;你所请求的页面有问题&quot;);<br> }<br> }<br> }</p> <pre><code> function checkUser(){ var Uname = document.myform.myname.value; alert(Uname); var psw = document.myform.mypsw.value; alert(psw); if(Uname==null){ window.alert(&quot;名字不能为空&quot;); document.myform.myname.focus(); return false; }else{ var url = &quot;LoginAction?myname=&quot;+Uname+&quot;&amp;mypsw=&quot;+psw;//应该是这里,为什么没起作用 sendRequest(url); System.out.println(&quot;2222222222222&quot;); } } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form action=&quot;&quot; method=&quot;post&quot; name = &quot;myform&quot;&gt; name:&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&lt;input type=&quot;text&quot; name=&quot;myname&quot; size=&quot;12&quot;&gt;&lt;p&gt; password:&lt;input type=&quot;password&quot; name=&quot;mypsw&quot; size=&quot;12&quot;&gt;&lt;p&gt; &lt;input type=&quot;button&quot; value=&quot;确定&quot; onclick=&quot;checkUser()&quot;&gt; &lt;/form&gt; &lt;/body&gt; </code></pre> <p></html></p> <p>2、这是Servlet页面</p> <p>package com.servlet;</p> <p>import java.io.IOException;<br> import java.io.PrintWriter;</p> <p>import javax.servlet.ServletException;<br> import javax.servlet.http.HttpServlet;<br> import javax.servlet.http.HttpServletRequest;<br> import javax.servlet.http.HttpServletResponse;</p> <p>public class LoginAction extends HttpServlet {</p> <pre><code>public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(&quot;进入servlet&quot;); request.setCharacterEncoding(&quot;UTF-8&quot;); String uname = request.getParameter(&quot;myname&quot;); String psw = request.getParameter(&quot;mypsw&quot;); response.setContentType(&quot;text/xml;charset=UTF-8&quot;); response.setHeader(&quot;Cache-Control&quot;, &quot;no-cache&quot;); PrintWriter out = response.getWriter(); out.println(&quot;&lt;response&gt;&quot;); if(uname.equals(&quot;amin&quot;)&amp;&amp;psw.equals(&quot;a&quot;)){ out.println(&quot;&lt;res&gt;&quot;+&quot;热烈欢迎AMIN&quot;+&quot;&lt;/res&gt;&quot;); }else{ out.println(&quot;&lt;res&gt;&quot;+&quot;对不起你登录失败&quot;+&quot;&lt;/res&gt;&quot;); } out.println(&quot;&lt;/response&gt;&quot;); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } public void init() throws ServletException { // Put your code here } </code></pre> <p>}<br> 3、这是Web.xml页面</p> <p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br> <web-app version="2.4" <br> xmlns="http://java.sun.com/xml/ns/j2ee" <br> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <br> xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee <br> http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><br> <servlet><br> <description>This is the description of my J2EE component</description><br> <display-name>This is the display name of my J2EE component</display-name><br> <servlet-name>LoginAction</servlet-name><br> <servlet-class>com.servlet.LoginAction</servlet-class><br> </servlet></p> <p><servlet-mapping><br> <servlet-name>LoginAction</servlet-name><br> <url-pattern>/LoginAction</url-pattern><br> </servlet-mapping><br> <welcome-file-list><br> <welcome-file>login.jsp</welcome-file><br> </welcome-file-list><br> </web-app></p> <p>可以说说为什么调用不了?</p>

2条回答 默认 最新

相关推荐 更多相似问题