服务器(servlet类)接不到数据,不知道是不是URL请求地址没写对

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



ajax请求响应方式
<br> var XMLHttpReq=false;<br> //创建XMLHttpRequest对象<br> function createXMLHttpRequest(){<br> if(window.XMLHttpRequest){<br> XMLHttpReq = new XMLHttpRequest();<br> if(XMLHttpReq.overrideMimetype)<br> {<br> XMLHttpReq.overrideMimeType(&#39;text/xml&#39;);<br><br> }</p> <pre><code> }else{ if(window.ActiveXObject) try{ XMLHttpReq = new ActiveXObject(&quot;Msxm12.XMLHTTP&quot;); }catch(e){ try{ XMLHttpReq = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;); }catch(e){ } } } } //处理服务器响应结果 function handleResponse(){ //判断对象状态 if(XMLHttpReq.readystate ==2){ //信息已经成功返回,开始处理信息 alert(&quot;d&quot;); if(XMLHttpReq.status == 200){ alert(&quot;a&quot;); var out=&quot;&quot;; var res =XMLHttpReq.responseXML; var response = res.getElementsByTagName(&quot;response&quot;)[0].firstchild.nodeVaule; document.getElementById(&quot;hello&quot;).innerHTML=response; } alert(&quot;c&quot;); }else{ alert(&quot;b&quot;); } } //发送客户端的请求 function sendRequest(url){ createXMLHttpRequest() var url=&#39;/a&#39;; XMLHttpReq.open(&quot;post&quot;,url,true); //响应指定函数 XMLHttpReq.onreadystatechange=handleResponse; //发送请求 XMLHttpReq.send(null); } //开始调用Ajax的功能 function sayHello(){ var name=document.getElementById(&quot;name&quot;).value; //发送请求 sendRequest(&quot;SayHello?name=&quot;+name); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;font size=&quot;1&quot;&gt;姓名:&lt;input type=&quot;text&quot; id=&quot;name&quot; &gt; &lt;input type=&quot;button&quot; value=&quot;提交&quot; onclick=&quot;sayHello()&quot; &gt; &lt;div id=&quot;hello&quot;&gt;&lt;/div&gt; &lt;/font&gt; &lt;/body&gt; </code></pre> <p></html><br> -------------------------------------------------------接下来是servlet处理数据代码<br> package servlets;</p> <p>import java.io.IOException;<br> import java.io.PrintWriter;</p> <p>import javax.servlet.ServletException;<br> import javax.servlet.annotation.WebServlet;<br> import javax.servlet.http.HttpServlet;<br> import javax.servlet.http.HttpServletRequest;<br> import javax.servlet.http.HttpServletResponse;</p> <p>/**</p> <ul> <li><p>Servlet implementation class sayHello<br> */<br> @WebServlet(&quot;/a&quot;)<br> public class ajaxTest extends HttpServlet {<br> private static final long serialVersionUID = 1L;</p> <p>/**</p> <ul> <li>@see HttpServlet#HttpServlet() */ public ajaxTest() { super(); // TODO Auto-generated constructor stub }</li> </ul> <p>/**</p> <ul> <li><p>@see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)<br> */<br> protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {<br> // TODO Auto-generated method stub</p> <p>//处理收到的参数,生成响应的XML文档</p></li> </ul> <p>}<br> /**</p> <ul> <li>@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); response.setContentType(&quot;text/xml;charset = GB2312&quot;); response.setHeader(&quot;Cache-control&quot;,&quot;no-cache&quot;); PrintWriter out = response.getWriter(); out.print(&quot;wewq&quot;); }</li> </ul></li> </ul> <p>}</p>

1个回答

没看到你的是servlet中有取数据的代码啊。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问