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