Post request URL appended with parameters not received, must be placed in the form of send ("use" =user)
and add
Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Servlet
1 PackageCn.itcast.controller;2 3 Importjava.io.IOException;4 Importjavax.servlet.ServletException;5 ImportJavax.servlet.annotation.WebServlet;6 ImportJavax.servlet.http.HttpServlet;7 Importjavax.servlet.http.HttpServletRequest;8 ImportJavax.servlet.http.HttpServletResponse;9 Ten@WebServlet ("/servlet/servletdemo2") One Public classServletDemo2extendsHttpServlet { A Private Static Final LongSerialversionuid = 1L; - - Public voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { the } - - Public voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { - //System.out.println ("ServletDemo2 doPost Running"); +String username = request.getparameter ("username"); -String Password = request.getparameter ("Password"); +System.out.println (username+ ":" +password); A } at -}
Jsp
1<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >234<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">5<title>ajax request test for POST method </title>67<body>8<input type= "button" Id= "B1" Name= "B1" value= "test communication with the server"/>9<div id= "D1" >Ten One</div> A<script type= "Text/javascript" > - -Window.onload=function () { thedocument.getElementById ("B1"). onclick =function () { - //get XMLHttpRequest Object -var xhr =createxmlhttprequest (); - //callback function for registering state changes +Xhr.onreadystatechange =function () { - if(Xhr.readystate = = 4) { + if(Xhr.status = = | | xhr.status = = 304) { A //don't do anything. at } - } - } - //initializes the XMLHttpRequest object, which is the open -Xhr.open ("POST", "/ajaxday02/servlet/servletdemo2?time=" +NewDate (). GetTime ()); - //sets the type of the request message header that tells the server to send the body data. inXhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); - //Send Data toXhr.send ("username=admin&password=123"); + } - } the function Createxmlhttprequest () { * var xmlHttp; $ Try{//Firefox, Opera 8.0+, SafariPanax Notoginsengxmlhttp=NewXMLHttpRequest (); -}Catch(e) { the Try{//Internet Explorer +xmlhttp=NewActiveXObject ("Msxml2.xmlhttp"); A}Catch(e) { the Try{ +xmlhttp=NewActiveXObject ("Microsoft.XMLHTTP"); -}Catch(e) {} $ } $ } - returnxmlHttp; - } the -</script>Wuyi</body> the