Ajax JS example

Source: Internet
Author: User

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 );}}}

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.