Here with an example to demonstrate the AJAX send GET request, instance specific requirements for a registration page, when the user fills out the user name, the input box loses focus will be sent to the background through AJAX authentication information, if the user name is not admin through authentication, or not through authentication.
Let's look at the JSP page for specific information:
<form action= "Servlet/loginservlet" method= "POST" > <table> <tr> <td> user account: </td> <td><input type= "text" name= "username" onblur= "checkUser (This)"/></td> < /tr> <tr> <td> user password:</td> <td><input type= "password" name= "password"/ ></td> </tr> <tr> <td><input type= "Submit" value= "Registration"/></td> <td><input type= "reset" value= "reset" ></td> </tr> </table> </ Form>
The background processing information here is handled by the servlet.
First look at the Web. XML configuration Information
<servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>login. loginservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name> loginservlet</servlet-name> <url-pattern>/servlet/LoginServlet</url-pattern> </ Servlet-mapping>
Then look at the Doget method of the specific servlet class
public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Response.setcontenttype ("Text/html;charaet=utf-8"); PrintWriter out = Response.getwriter (); String Name=request.getparameter ("username"); SYSTEM.OUT.PRINTLN (name), if (Name.equals ("admin")) Out.print (false); Elseout.print (true); Out.flush (); Out.close ();}
Do a simple validation in the Servlet class.
In the JSP form, the input box for the input user name is set to an event that loses focus, that is, the onblur event. Look at the JavaScript code below.
<script type= "Text/javascript" >//create Xmlhttprequestfunction createxmlhttprequest () {if (window. XMLHttpRequest) {return new XMLHttpRequest ();} Else{return new ActiveXObject ("Microsoft.XMLHTTP");}} When the user account input box loses focus, call the method function CheckUser (obj) {//Gets the value of input box input var user = obj.value;//If the value in the input box is empty, then pop-up prompts and let the input box get the focus if (! User) {alert ("Username cannot be empty! "); Obj.focus (); return;} When not empty, use an AJAX request to send information to the background to verify that the user name is available//get request string var url= "Servlet/loginservlet?username=" +user;// Call method to create XMLHttpRequest object XMLHttpRequest = Createxmlhttprequest ();//Set callback function xmlhttprequest.onreadystatechange=finish;/ /Initialize Xmlhttprequestxmlhttprequest.open ("GET", url,true);//Send request xmlhttprequest.send (null);} Callback functions function Finish () {if (xmlhttprequest.readystate = = 4&& Xmlhttprequest.status = =) {var result = Xmlhttprequest.responsetext;if (Result = = "true") {alert ("username available!") ");} Else{alert ("User name is not available!) ");}}} </script>
Ajax sends a GET request