HTML page
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML>
Javascript:
VaR XMLHTTP; function varify () {var username = document. getelementbyid ("username "). value; If (window. XMLHttpRequest) {XMLHTTP = new XMLHttpRequest (); If (XMLHTTP. overridemimetype) {XMLHTTP. overridemimetype ("text/XML") ;}} else if (window. activexobject) {var activexname = ["MSXML. XMLHTTP "," Microsoft. XMLHTTP "]; for (var I; I <activexname. length; I ++) {try {XMLHTTP = new activexobject (activexname [I]); break ;} Catch (e) {}}// when registering a callback function, you only need the function name and do not need to enclose XMLHTTP. onreadystatechange = callback;/** the first parameter indicates the HTTP Request Method and supports all HTTP request methods, mainly get and post. * The second parameter indicates the request URL address, get parameters are also in the URL; * The third parameter indicates asynchronous or synchronous interaction, and true indicates asynchronous; * // XMLHTTP. open ("get", "ajaxservlet? Name = "+ username, true); // XMLHTTP. send (null); // post XMLHTTP. open ("Post", "ajaxservlet", true); XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded"); // The XMLHTTP parameter. send ("name =" + username);} // callback function callback () {// you can call this function to determine if (XMLHTTP. readystate = 4) {If (XMLHTTP. status = 200) {// use the plain text method out. println ("username [" + name + "] already exists. Please use another username,"); var responsetext = XMLHTTP. responsetext; alert (responsetext); // use xml s. append ("user name [" + name + "] already exists. Please use another user name ,"). append ("</message>"); var domobj = XMLHTTP. responsexml; var messagenode = domobj. getelementsbytagname ("message"); var responsemessage = messagenode [0]. firstchild. nodevalue; alert (responsemessage); document. getelementbyid ("MSG "). innerhtml = responsemessage ;}}}
============================
Ajaxservlet
Import javax. servlet. servletexception; import javax. servlet. HTTP. *; import Java. io. *; public class ajaxservlet extends httpservlet {protected void dopost (httpservletrequest request, incluresponse) throws servletexception, ioexception {doget (request, response);} protected void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("text/XML; charset = UTF-8"); // use XML as text/XML printwriter out = response. getwriter (); string name = request. getparameter ("name"); stringbuilder S = new stringbuilder (); S. append ("<message>"); If (name = NULL | Name. length () = 0) {S. append ("user cannot be blank "). append ("</message>");} else {If (name. equals ("QQ") {S. append ("user name [" + name + "] already exists. Please use another user name ,"). append ("</message>");} else {S. append ("user name [" + name + "] does not exist, you can use "). append ("</message>");} Out. println (S. tostring (); system. out. println (s );}}}