Ajax asynchronous processing when a user applies for a new user

Source: Internet
Author: User

Ajax asynchronous processing when a user applies for a new user, after the user name is entered, the system prompts whether the user name is registered after you click somewhere else:

 

JS Code processed by Ajax:

<Script language = "JavaScript">

VaR xmlhttpreq;
Function createxmlhttprequest (){
If (window. XMLHttpRequest ){
Xmlhttpreq = new XMLHttpRequest ();
} Else {
Xmlhttpreq = new activexobject ("Microsoft. XMLHTTP ");
}
}
Function checkuser (){
Createxmlhttprequest ();
Xmlhttpreq. onreadystatechange = handle;
VaR username = Document. getelementbyid ("username"). value;
VaR test_username = Document. getelementbyid ("test_username ");
Test_username.style.display = "NONE ";
If (username = ""){
Test_username.style.display = "Block ";
Alert ("username can not be empty ");
} Else {

// The URL is a jump address, which transfers the user name to the action layer and calls the checkuser Method for verification.
VaR url = "system/login-checkuser? Username = "+ username +" & time = "+ new date (). gettime ();
Xmlhttpreq. Open ("get", URL, true );
Xmlhttpreq. onreadystatechange = handle;
Xmlhttpreq. Send (null );
}
}
Function handle (){
If (xmlhttpreq. readystate = 4 ){
If (xmlhttpreq. Status = 200 ){
VaR res = xmlhttpreq. responsetext;
VaR result = Document. getelementbyid ("test_username ");
Result. style. Display = "";
Result. innerhtml = res; // sets the prompt information.
}
}
}

</SCRIPT>

 

JSP page call:

<Tr>
<TD width = "104"> <Div align = "right"> Username: $ {userexist} </div> </TD>
<TD width = "201" Height = "24"> <input name = "userdto. username "type =" text "class =" inputcontent "id =" username "value =" "onblur =" checkuser () "size =" 20 ">
<SPAN class = "star"> * </span> </TD>
<TD width = "473"> <Div id = "test_username" style = "display: none "> <font color =" # ff0000 "> the user name cannot be blank. </font> </div> </TD>
</Tr>

 

The action layer verifies whether the user has the following methods:

 

/** Verify whether the user name exists **/
Public void checkuser (){
Try {
Httpservletrequest request = servletactioncontext. getrequest ();
Httpservletresponse response = servletactioncontext. getresponse ();
Printwriter PW = response. getwriter ();
String username = request. getparameter ("username ");
If (usermanagebiz. checkuser (username )){
PW. println ("<font color = 'red'> the user name already exists. Please enter it again! </Font> <input type = 'den den 'name = 'userexist' value = 'userexist'/> ");
} Else PW. println ("<font color = 'blue'> congratulations, you can use this user name! </Font> ");

} Catch (ioexception e ){
E. printstacktrace ();
}
}

 

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.