[Ajax entry] Asynchronous User Name Verification Using ajax

Source: Internet
Author: User

[Function description] We usually need to enter the user name when registering a website. However, if we enter the user name we like, we can leave the input box and sometimes remind you that the user name has been registered, however, other content on the webpage is not updated. This is asynchronous implementation.

The following describes how to implement it. (Ajax + servlet)

Front-end code:

<Div id = "register" class = "div_cntent"> <form name = "form1"> <Table> <tr> <TH align = "center" colspan = "2"> user Registration </Th> </tr> <TD> User Name: </TD> <input type = "text" name = "reg_name" id = "reg_name" onblur = "checkname () "/> <span id =" name_tip "> </span> </TD> </tr> <TD> password: </TD> <input type = "password" name = "reg_password" id = "reg_password"/> <span id = "password_tip"> </span> </ TD> </tr> <TD> <input type = "button" value = "register" class = "btn_style"/> </TD> <Input type = "reset" value = "reset" class = "btn_style"/> </TD> </tr> </table> </form> </div>

Using onblur = "checkname ()" indicates that the mouse calls the checkname () method when the user name text box loses focus.


JS Code

/** Ajax asynchronous detection username **/function checkname () {var xmlhttpchk; // the XMLHTTPRequest object that is used to exchange data with the server in the background. VaR reg_name = document. getelementbyid ("reg_name "). value; // determine the browser's problem: different browsers create different XMLHTTP Methods // all modern browsers support XMLHttpRequest objects (ie5 and IE6 use activexobject ). If (window. XMLHttpRequest) {xmlhttpchk = new XMLHttpRequest (); // Firefox, Google, opera ......} Else {xmlhttpchk = new activexobject (); // ie5 IE6 ......} // Specify that the xmlhttpchk. onreadystatechange = function () {/** contains the XMLHttpRequest status when the response is in the ready state of the onreadystatechange event. Changes from 0 to 4. * 0: the request is not initialized. * 1: The server connection has been established. * 2: The request has been received. * 3: The request is being processed. * 4: The request has been completed, the response is ready * status * 200: OK * 400: "Page not found" **/If (xmlhttpchk. readystate = 4 & xmlhttpchk. status = 200) {alert (xmlhttpchk. responsetext); // to use the jsonobject object, you must use the eval () method to convert the returned data to the object var resultobj = eval ("(" + xmlhttpchk. responsetext + ")"); If (resultobj. exist) {// the user name already exists in document. getelementbyid ("name_tip "). innerhtml = "<font color = '# f00'> User Name Already exists </font> "; // user name available} else {document. getelementbyid ("name_tip "). innerhtml = "<font color = '#0f0'> User Name available </font>" ;}}; // if you need to send the request to the server, we use the open () and send () Methods of the XMLHTTPRequest object: xmlhttpchk. open ("get", "checkname? Reg_name = "+ reg_name, true); xmlhttpchk. Send ();/* Open (method, URL, async) specifies the request type, URL, and whether to asynchronously process the request. Method: Request type; get or posturl: Location of the file on the server async: true (asynchronous) or false (synchronous) Send (string) to send the request to the server. String: only used for post requests */}

The code above is to use Ajax asynchronous Implementation of user name verification

VaR xmlhttpchk; var reg_name = document. getelementbyid ("reg_name "). value; // judge the browser's problem if (window. XMLHttpRequest) {xmlhttpchk = new XMLHttpRequest ();} else {xmlhttpchk = new activexobject ();}


Background code

@ Override protected void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {This. dopost (request, response) ;}@ override protected void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("text/html; charset = UTF-8"); printwriter out = response. getwriter (); string reg_name = request. getparameter ("reg_name"); reg_name = new string (reg_name.getbytes ("ISO8859-1"), "UTF-8"); system. out. println (reg_name); // create a JSON object jsonobject resultjson = new jsonobject (); If ("Allen ". equals (reg_name) {resultjson. put ("exist", true); // indicates that the user name already exists} else {resultjson. put ("exist", false); // indicates that the user name does not exist} Out. print (resultjson); out. flush (); out. close ();}


Finally, configure web. xml and copy the following files to the Lib file.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/47/E4/wKiom1QC6v6is4PHAACD4tzkhvU573.jpg "Title =" json-lib.jpg "alt =" wkiom1qc6v6is4phaacd4tzkhvu573.jpg "/> download here

<! -- User verification --> <servlet-Name> checknameservlet </servlet-Name> <servlet-class> COM. ajax. web. checknameservlet </servlet-class> </servlet> <servlet-mapping> <servlet-Name> checknameservlet </servlet-Name> <URL-pattern>/checkname </url-Pattern> </servlet-mapping>


This article is from the "henrry" blog, please be sure to keep this source http://erlong.blog.51cto.com/6669354/1547127

[Ajax entry] Asynchronous User Name Verification Using ajax

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.