Javascript request servlet implementation ajax example (SHARE), servletajax

Source: Internet
Author: User

Javascript request servlet implementation ajax example (SHARE), servletajax

Ajax requests are a new and refreshing user experience. They can send GET and POST asynchronous requests. The record is as follows:

GET request:

Function sendRequestByGet () {// defines the asynchronous request object var xmlReq; // checks whether the browser directly supports ajax if (window. XMLHttpRequest) {// ajax xmlReq = new XMLHttpRequest ();} else {// ajax xmlReq = new ActiveObject ('Microsoft. XMLHTTP ');} // sets the callback function xmlReq. onreadystatechange = function () {if (xmlReq. readyState = 4 & xmlReq. status = 200) {// obtain the server response value var result = xmlReq. responseText; // subsequent operations alert (result) ;}; // create an asynchronous get request var url = "Hello? Name = zhangsan "; xmlReq. open (" GET ", url, true); // send the request xmlReq. send (null );}

POST request:

Function sendRequestByPost () {// defines the asynchronous request object var xmlReq; // checks whether the browser directly supports ajax if (window. XMLHttpRequest) {// ajax xmlReq = new XMLHttpRequest ();} else {// ajax xmlReq = new ActiveObject ('Microsoft. XMLHTTP ');} // sets the callback function xmlReq. onreadystatechange = function () {if (xmlReq. readyState = 4 & xmlReq. status = 200) {// obtain the server response value var result = xmlReq. responseText; // subsequent operations alert (result) ;}; // create an asynchronous Post request var url = "Hello"; xmlReq. open ("POST", url, true); xmlReq. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded"); // send the request var data = "name = lisi"; xmlReq. send (data );}

Ajax request servlet:

@Override  protected void doPost(HttpServletRequest req, HttpServletResponse resp)      throws ServletException, IOException {    String name=req.getParameter("name");    PrintWriter out = resp.getWriter();    out.print(name);  }

Effect:

The above javascript request servlet implementation ajax example (SHARE) is all the content shared by the editor. I hope you can give us a reference, and hope you can also support the help house.

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.