Use Ajax in struts2 to verify whether a mailbox (user name) is registered

Source: Internet
Author: User


Action class:

Package COM. grace. myblog. view. action; import Java. io. printwriter; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. apache. struts2.servletactioncontext; import Org. springframework. stereotype. controller; import COM. grace. myblog. domain. user; @ controllerpublic class emailcheckaction {/*****/Private Static final long serialversionuid = 1l; Public String execute () throws exception {try {string responsetext = ""; httpservletrequest request = servletactioncontext. getrequest (); string email = request. getparameter ("email"); system. out. println ("---" + email); User user = registerservice. findbyemail (email); // query the database by email to determine whether the mailbox has been registered if (user = NULL) {responsetext = "2 ";} else {responsetext = "1";} httpservletresponse response = servletactioncontext. getresponse (); response. setcontenttype ("text/html; charset = UTF-8"); printwriter out = response. getwriter (); out. print (responsetext); out. flush (); out. close ();} catch (exception e) {e. printstacktrace ();} return NULL ;}}

Code in JS

 
Function checkusermail () {/* Email check Ajax */var email = document. getelementbyid ("email "). value; var xhr = ajaxfunction (); xhr. onreadystatechange = function () {If (xhr. readystate = 4) {If (xhr. status = 200) {var DATA = xhr. responsetext; // obtain the text if (Data = "1") {document. getelementbyid ("emailcheck "). innerhtml = "<font color = 'red'> this email address has been registered </font>"; return false;} else {document. getelementbyid ("emailcheck "). innerhtml =" Hi, this email has not been registered "; return true ;}}} xhr. Open (" get "," emailcheck. Action? Email = "+ email, true); xhr. setRequestHeader (" Content-Type "," application/X-WWW-form-urlencoded "); xhr. Send ();}
Function ajaxfunction () {var XMLHTTP; try {// Firefox, opera 8.0 +, Safari XMLHTTP = new XMLHttpRequest ();} catch (E) {try {// Internet Explorer XMLHTTP = new activexobject ("msxml2.xmlhttp");} catch (e) {try {XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");} catch (e) {}} return XMLHTTP ;}


Sturts. xml configuration

 
<! -- Email check Ajax --> <action name = "emailcheck" class = "emailcheckaction">/* use the annotation method. Therefore, the class name is lowercase by default */</Action>

You only need to add the following line of code to the JSP page where you want to display the prompt.

 
<Div id = "emailcheck"> </div>



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.