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.