The effect is as follows:
Server-side
Copy Code code as follows:
Package com.ljq.test;
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;
/**
* Use jquery to accept and process XML data
*
* @author Jiqinlin
*
*/
@SuppressWarnings ("Serial")
public class Ajaxxmlserver extends httpservlet{
protected void doget (HttpServletRequest request,
HttpServletResponse response) throws Servletexception, IOException {
try{
The Content-type to modify a----response must be text/xml
Response.setcontenttype ("Text/xml;charset=utf-8");
PrintWriter out = Response.getwriter ();
1. Take parameters
String old = Request.getparameter ("name"). Trim ();
Modifying the data returned by the two-----needs to be assembled into an XML format
StringBuilder builder = new StringBuilder ();
Builder.append ("<message>");
2. Check the parameter is not a problem
if (old = null | | "". Equals (old)) {
Builder.append ("User name cannot be empty"). Append ("</message>");
} else{
3. Check operation
String name = old;
if (Name.equals ("Linjiqin")) {
Builder.append ("User name [" + name +] already exists, please use a different user name "). Append (" </message> ");
} else{
Builder.append ("User name [" + name + "] does not exist, you can use the user name to register"). Append ("</message>");
}
Out.println (Builder.tostring ());
System.out.println (Builder.tostring ());
}
catch (Exception e) {
E.printstacktrace ();
}
}
protected void DoPost (HttpServletRequest request,
HttpServletResponse response) throws Servletexception, IOException {
Doget (request, response);
}
}
Modify Web.xml
Copy Code code as follows:
<servlet>
<servlet-name>AjaxXmlServer</servlet-name>
<servlet-class>com.ljq.test.AjaxXmlServer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AjaxXmlServer</servlet-name>
<url-pattern>/servlet/ajaxXmlServer</url-pattern>
</servlet-mapping>
Request page
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"
"Http://www.w3.org/TR/html4/loose.dtd" >
<script type= "Text/javascript" src= "Http://localhost:8083/jqueryprj/js/jquery-1.3.1.js" ></script>
<script type= "Text/javascript" src= "Http://localhost:8083/jqueryprj/js/verifyjqueryxml.js" ></script>
<body>
<input type= "text" id= "UserName"/>
<input type= "button" value= "checksum" onclick= "verify ()"/>
<div id= "Result" ></div>
<!--the intuitive difference between div and span, the content in the Div is exclusive, the content in span and the other content get along well-->
</body>
Js
Copy Code code as follows:
function Verify () {
var userName = $ ("#userName"). Val ();
$.ajax ({
Type: "POST",//http request method
URL: "Servlet/ajaxxmlserver",//server-side URL address
Data: "Name=" + userName,//Sent to server-side
DataType: "xml",//telling jquery the data format returned
Success:callback//Defines the callback function that is called when the interaction completes and the server returns data correctly
});
}
callback function
function callback (data) {
var jqueryobj = $ (data);
Get Message Node
var message = Jqueryobj.children ();
Get text content
var text = Message.Text ();
4. The data returned by the server segment is displayed dynamically on the page
$ ("#result"). html (text);
}