My personal understanding is as follows !!!
1. Ajax is divided into page (front-end) and background processing (backend, Servlet );
2. Page: Create an XMLHTTPRequest object. The supported XMLHttpRequest objects are unavailable in different browsers;
Page: Create the sending function XMLHttpRequest. Open ("get", URL, true );
XMLHttpRequest. onreadystatechange = processresponse;
XMLHttpRequest. Send (null); // XMLHttpRequest. Send ("uname =" + "LC ")
In the open function, the first parameter indicates the request method. There are two types: Get and post.
The second parameter is the webpage or servlet to which the request is submitted.
The third parameter: True indicates asynchronous
In the send function: indicates the parameter submitted to the servlet. It can be a simple key-Value Pair parameter, or the entire XML document
When a request is submitted using the get method, is the parameter followed by URL or login. do? Uname = Lc;
When submitting using the POST method, you can submit complex data, such as XML documents,
However, you need to follow up when submitting a POST request. XMLHttpRequest. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
3. Page: Create a callback function, processresponse (), used to process data returned from the Servlet
If (xmlhttpreq. readystate = 4 ){
If (xmlhttpreq. Status = 200 ){
VaR res = xmlhttpreq. responsetext;
Window. Alert (RES );
} Else {
Window. Alert ("the page you requested has an exception! ");
}
}
4. Page: The returned data can be plain text: XMLHttpRequest. responsetext;
It can also be an XML document: XMLHttpRequest. responsexml;
5. Page: display the returned data to the HTML/JSP page through document. All. a00.innerhtml ();
You can also directly operate on DOM objects and add nodes to HTML/JSP.
6. Background: The foreground uses get + simple parameters. Background Processing Method
String uname = Req. getparameter ("uname ");
Printwriter out = resp. getwriter ();
Out. println ("<response> ");
If ("LC". Equals (uname) & "LC". Equals (password )){
Out. println ("<res> thank you! </RES> ");
} Else {
Out. println ("<res> sorry, login failed! </RES> ");
}
Out. println ("</response> ");
Out. Close ();
Note: The plain text or XML document is returned at the front end. The XML document is used here.
7. Background: The foreground uses the post + XML parameter. Background Processing Method
Stringbuilder bxml = new stringbuilder ();
String line = NULL;
Try {
Bufferedreader reader = Req. getreader ();
While (line = reader. Readline ())! = NULL ){
Bxml. append (line );
}
} Catch (exception e ){
E. printstacktrace ();
}
String xml = bxml. tostring ();
Xml = urldecoder. Decode (XML, "utf8"); // garbled during submission. This is a solution
Document xmldoc = NULL;
Try {
Xmldoc = documentbuilderfactory. newinstance (). newdocumentbuilder ()
. Parse (New bytearrayinputstream (XML. getbytes ()));
} Catch (exception e ){
E. printstacktrace ();
}
String name = xmldoc. getelementsbytagname ("name"). Item (0). getfirstchild (). getnodevalue ();
String responsetext = "";
Printwriter out = resp. getwriter ();
If ("LC". Equals (name) & "LC". Equals (psw )){
Responsetext = "Welcome! ";
} Else {
Responsetext = "sorry, login failed! ";
}
// Set the format and Character Set of output information
Resp. setcharacterencoding ("UTF-8 ");
Resp. setheader ("charset", "UTF-8 ");
Resp. setcontenttype ("text/XML; charset = UTF-8 ");
Out. Write (responsetext );
Out. Close ();
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/aaabendan/archive/2010/02/23/5319596.aspx