Ajax asynchronous Communication Small example servlet and JSP asynchronous post method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.