Abstract: In this section, add ajax Study Notes 1.
Method 2: Use the responseXML method of the XMLHttpRequest object to accept the DOM object of the XML data object.
In the ajax study Note 1, you have made a detailed introduction to the preparation and required knowledge. This section describes the code to be modified and the newly added code.
. Added a servlet class.
AJAXXMLServer. java
Copy codeThe Code is as follows: import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import javax. servlet. ServletException;
Import java. io. IOException;
Import java. io. PrintWriter;
// XML data
Public class AJAXXMLServer extends HttpServlet {
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
// Response. setContentType ("text/html; charset = UTF-8 ");
Response. setContentType ("text/xml; charset = UTF-8"); // modify this time to text/xml
PrintWriter out = response. getWriter ();
// 1. Take the Parameter
String old = request. getParameter ("name ");
StringBuffer sb = new StringBuffer ();
Sb. append ("<message> ");
// 2. Check for any problems
If (old = null | old. length () = 0 ){
Sb. append ("user name cannot be blank"). append ("</message>"); // assemble XML
} Else {
// 3. Verify the operation
String name = old;
If (name. equals ("pan ")){
// 4. Different from traditional applications. In this step, you need to return the data you are interested in to the page, instead of sending a new page to the user.
// The writing method has not changed, and the essence has changed
Sb. append ("user name [" + name + "] already exists"). append ("</message>"); // assemble XML
} Else {
Sb. append ("user name [" + name + "] available"). append ("</message>"); // assemble XML
}
}
Out. println (sb. toString (); // note that this sentence must not be missing, and pay attention to the placement location.
}
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
This. doGet (request, response );
}
}
. Modify web. xml
Add the AJAXXMLServer class configuration
Web. xmlCopy codeThe Code is as follows: <servlet>
<Servlet-name> AJAXXMLServer </servlet-name>
<Servlet-class> AJAXXMLServer </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> AJAXXMLServer </servlet-name>
<Url-pattern>/AJAXXMLServer </url-pattern>
</Servlet-mapping>
. Modify the verify. js File
Article 1:Copy codeThe Code is as follows: Put xmlhttp. open ("GET", "AJAXServer? Name = "+ username, true );
ChangeCopy codeThe Code is as follows: xmlhttp. open ("GET", "AJAXXMLServer? Name = "+ username, true); // responseXML method, modify the class name
Second:
Set
ResponseTextCopy codeThe Code is as follows: // obtain the data returned by the server
// Method 1: Obtain plain text data output by the server
// Var responseText = xmlhttp. responseText;
// Display the data on the page and find the Element Node corresponding to the div label through dom
// Var divNode = document. getElementById ("result ");
// Set the html content in the Element Node
// DivNode. innerHTML = responseText;
Changed:
ResponseXMLCopy codeThe Code is as follows: // Method 2: Use responseXML to accept DOM objects of XML Data Objects
Var domObj = xmlhttp. responseXML;
Var messageNodes = domObj. getElementsByTagName ("message ");
// Obtain the text content in the message Node
// The text in the message label is the child node of the element node corresponding to the message label in the dom. firstChild can obtain the first child node of the current node.
If (messageNodes. length> 0 ){
Var textNode = messageNodes [0]. firstChild;
// For a text node, you can use nodeValue to return the text
Var responseMessage = textNode. nodeValue;
// Display the value responseMessage on the div
Var divNode = document. getElementById ("result ");
DivNode. innerHTML = responseMessage;
} Else {
Alert ("XML data format error, original text content:" + xmlhttp. responseText );
}
Third:
Set
Text/htmlCopy codeThe Code is as follows: if (xmlhttp. overrideMimeType ){
Xmlhttp. overrideMimeType ("text/html ");
}
Changed:
Text/xmlCopy codeThe Code is as follows: if (xmlhttp. overrideMimeType ){
Xmlhttp. overrideMimeType ("text/xml"); // you need to modify the XML format.
}
For ie browsers, if the third part is not modified, no error is reported. For firefox browsers, if the third part is not modified, the following statement will report an error.
Var domObj = xmlhttp. responseXML;
You can add an alert statement to verify that the if statement of the Code modified in the third part will not be executed in IE, And the if statement will be executed in firefox.
Note: The access path, running, and ajax learning Note 1 are all already in use and are omitted.