Summary: This section complements Ajax learning Note 1
The second way: to accept the DOM object of an XML data object by using the Responsexml of the XMLHttpRequest object
In Ajax learning Note 1, there is a more detailed introduction to the preparation and the knowledge that needs to be used, and this section focuses on the code that needs to be modified and the new code
. Add a Servlet class
Ajaxxmlserver.java
Copy Code code 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;
Data FOR XML
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 to Text/xml
PrintWriter Out=response.getwriter ();
1. Take parameters
String old=request.getparameter ("name");
StringBuffer sb=new StringBuffer ();
Sb.append ("<message>");
2. Check if there are any problems
if (old==null| | Old.length () ==0) {
Sb.append ("User name cannot be empty"). Append ("</message>"); Assembling XML
}else{
3. Check operation
String Name=old;
if (Name.equals ("Pan")) {
4. Different from the traditional application. Instead of sending a new page to the user, this step needs to return the data that the user is interested in to the page end.
The writing has not changed, the essence has changed
Sb.append ("User name [" +name+] already exists "). Append (" </message> "); Assembling XML
}else{
Sb.append ("User name [" +name+] can be used "). Append (" </message> "); Assembling XML
}
}
Out.println (Sb.tostring ()); Note that the sentence must not be less, and pay attention to place
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
This.doget (Request,response);
}
}
. Modify Web.xml
Join the Ajaxxmlserver class configuration
Xml
Copy Code code 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
First place:
Copy Code code as follows:
Put Xmlhttp.open ("Get", "ajaxserver?name=" +username,true);
To
Copy Code code as follows:
Xmlhttp.open ("Get", "Ajaxxmlserver?name=" +username,true),//responsexml way, modify class name
Second place:
Put
ResponseText
Copy Code code as follows:
Getting data returned by the server side
The first way: Get the server-side output of plain text data
var Responsetext=xmlhttp.responsetext;
Display the data on the page by Dom to find the element node for the DIV tag
var Divnode=document.getelementbyid ("result");
To set the contents of HTML in an element node
Divnode.innerhtml=responsetext;
To
Responsexml
Copy Code code as follows:
The second way: to accept the DOM object of an XML data object using the Responsexml method
var domobj = Xmlhttp.responsexml;
var messagenodes = domobj.getelementsbytagname ("message");
Get the text content in the message node
The text in the message label is a child of the element node in the DOM that corresponds to the message label, and FirstChild can get the first child node of the current node
if (Messagenodes.length > 0) {
var textnode = Messagenodes[0].firstchild;
For text nodes, you can return text in a nodevalue way
var responsemessage = Textnode.nodevalue;
To 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 place:
Put
Text/html
Copy Code code as follows:
if (Xmlhttp.overridemimetype) {
Xmlhttp.overridemimetype ("text/html");
}
To
Text/xml
Copy Code code as follows:
if (Xmlhttp.overridemimetype) {
Xmlhttp.overridemimetype ("Text/xml");//xml the way you need to modify this place
}
For IE browser, if the third is not modified will not error, but for Firefox browser, if not modified here, the following statement will error
var domobj = Xmlhttp.responsexml;
You can verify by adding alert statements that the IF statement of the third modified code in IE is not executed, and the IF statement is executed by the Firefox browser.
Description: Access path, run screenshots and Ajax Learning Notes 1 are already, and then omitted.