Ajax send GET request
Here with an example of Ajax send GET request, the instance specific requirements for a registration page, when the user filled out the user name, the input box after the loss of focus will be through Ajax to the background to send authentication information, if the user name is not admin to pass validation, otherwise not through validation.
First look at the JSP page specific information:
The code is as follows |
Copy Code |
<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= "registered"/></td> <td><input type= "reset" value= "reset" ></td> </tr> </table> </form> |
The background processing information here is handled by a servlet
First look at the Web.xml configuration information
The code is as follows |
Copy Code |
<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
The code is as follows |
Copy Code |
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); Else Out.print (TRUE); Out.flush (); Out.close (); } |
Do a simple validation in the Servlet class.
In the JSP form, the event that loses focus is set on the input box for the input user name, which is the onblur event. Look at the JavaScript code below.
The code is as follows |
Copy Code |
<script type= "Text/javascript" > Create XMLHttpRequest function Createxmlhttprequest () { if (window. XMLHttpRequest) { return new XMLHttpRequest (); }else{ return new ActiveXObject ("Microsoft.XMLHTTP"); } } Call this method when the user account input box loses focus function CheckUser (obj) { Gets the value entered in the input box var user = Obj.value; If the value in the input box is blank, the window prompts and the input box gets the focus if (!user) { Alert ("User name cannot be empty!") "); Obj.focus (); Return } When not empty, use AJAX requests 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 Create XMLHttpRequest Object XMLHttpRequest = Createxmlhttprequest (); Set callback function Xmlhttprequest.onreadystatechange=finish; Initialize XMLHttpRequest Xmlhttprequest.open ("Get", url,true); Send Request Xmlhttprequest.send (NULL); } callback function function Finish () { if (xmlhttprequest.readystate = = 4&& Xmlhttprequest.status = = 200) { var result = Xmlhttprequest.responsetext; if (result = = "true") { Alert ("User name is available!") "); }else{ Alert ("User name is not available!") "); } } } </script> |
Ajax send GET request
Here with an example of Ajax send GET request, the instance specific requirements for a registration page, when the user filled out the user name, the input box after the loss of focus will be through Ajax to the background to send authentication information, if the user name is not admin to pass validation, otherwise not through validation.
First look at the JSP page specific information:
The code is as follows |
Copy Code |
<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= "registered"/></td> <td><input type= "reset" value= "reset" ></td> </tr> </table> </form> |
The background processing information here is handled by a servlet
First look at the Web.xml configuration information
The code is as follows |
Copy Code |
<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
The code is as follows |
Copy Code |
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); Else Out.print (TRUE); Out.flush (); Out.close (); } |
Do a simple validation in the Servlet class.
In the JSP form, the event that loses focus is set on the input box for the input user name, which is the onblur event. Look at the JavaScript code below.
The code is as follows |
Copy Code |
<script type= "Text/javascript" > Create XMLHttpRequest function Createxmlhttprequest () { if (window. XMLHttpRequest) { return new XMLHttpRequest (); }else{ return new ActiveXObject ("Microsoft.XMLHTTP"); } } Call this method when the user account input box loses focus function CheckUser (obj) { Gets the value entered in the input box var user = Obj.value; If the value in the input box is blank, the window prompts and the input box gets the focus if (!user) { Alert ("User name cannot be empty!") "); Obj.focus (); Return } When not empty, use AJAX requests 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 Create XMLHttpRequest Object XMLHttpRequest = Createxmlhttprequest (); Set callback function Xmlhttprequest.onreadystatechange=finish; Initialize XMLHttpRequest Xmlhttprequest.open ("Get", url,true); Send Request Xmlhttprequest.send (NULL); } callback function function Finish () { if (xmlhttprequest.readystate = = 4&& Xmlhttprequest.status = = 200) { var result = Xmlhttprequest.responsetext; if (result = = "true") { Alert ("User name is available!") "); }else{ Alert ("User name is not available!") "); } } } </script> |