JS Code:
1<script language= "JavaScript" >2 var req;3 function Createreq () {4 if(window. XMLHttpRequest) {5req =NewXMLHttpRequest ();6}Else if(window. ActiveXObject) {7req =NewActiveXObject ("Microsoft.XMLHTTP");8 }9 }Ten One function Validate () { A createreq (); -var id = document.getElementById ("CID"); -var url = "Validate.jsp?id=" +Escape (id.value); theReq.open ("Get", URL,true); -Req.onreadystatechange =callback; - req.send (); - } + - function Callback () { + if(Req.readystate = = 4) { A if(Req.status = = 200) { atvar msg = document.getElementById ("msg")); -msg.innerhtml =Req.responsetext; - } - } - } -</script>
User-registered JSP page key HTML code
1 <!--input user name text box--2 <input type= "text" id= "CID" name= "name" onblur= "Validate ()"/>3 <!--display information span--4 <span id= "msg" style= "color:red;" ></span>
Code for the asynchronous request that is processed in the background (validate.jsp)
1<%2String id = request.getparameter ("id");3 if(id! =NULL) {4 if(Customermgr.getinstance (). Existname (ID)) {5Out.write ("The user name already exists!"));6}Else {7Out.write ("Congratulations, you can use the user name!"));8 }9 Ten } One%>
Experience: When the user name text box is out of focus (onblur event), an asynchronous request object created by JS creates a request to the server and passes in the user name parameter, if the server is ready, receives the request and processes it, and returns the response information back to the client.
JSP---Online shop, using Ajax technology to verify that the user name already exists