Describes how to Implement Asynchronous verification and javascript verification using the traditional javascript method.
When learning JavaScript asynchronous verification, we often start with the most traditional XMLHttpRequest. This article will talk about the understanding of traditional verification:
Code 1index. jsp file:
<%@ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <% String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + request. getContextPath () + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
Code 2demo1. jsp file:
<%@ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <% String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + request. getContextPath () + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
Code 3demo2. jsp file:
<%@ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <% String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + request. getContextPath () + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
Code 4AjaxCheckUserNameServlet. java file:
Package com. ghj. packagofserlet; import java. io. IOException; import java. io. printWriter; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class AjaxCheckUserNameServlet extends HttpServlet {private static final long serialVersionUID = 6387744976765210524L;/*** process demo1.j Asynchronous verification ** @ author GaoHuanjie */public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {try {response. setCharacterEncoding ("UTF-8"); request. setCharacterEncoding ("UTF-8"); PrintWriter out = response. getWriter (); // System. out. println (1/0); // check xmlHttpRequest in demo1.jsp if an exception occurs intentionally. status! String userName = request. getParameter ("userName"); // obtain the "user name" System. out. println ("Asynchronous Authentication in demo1.jsp processing, userName:" + userName); if ("admin ". equals (userName) {out. print ("1"); // "1" indicates that the user name is unavailable.} Else {out. print ("2"); // "2" indicates that the user name is available.} Out. flush (); out. close ();} catch (Exception e) {e. printStackTrace (); response. setStatus (405) ;}/ *** process asynchronous verification in demo2.jsp ** @ author GaoHuanjie */public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {try {response. setCharacterEncoding ("UTF-8"); request. setCharacterEncoding ("UTF-8"); PrintWriter out = response. getWriter (); // System. ou T. println (1/0); // deliberately exception occurs to check xmlHttpRequest. status in demo2.jsp! String userName = request. getParameter ("userName"); // obtain the "user name" System. out. println ("process asynchronous verification in demo2.jsp, userName:" + userName); if ("admin ". equals (userName) {out. print ("1"); // "1" indicates that the user name is unavailable.} Else {out. print ("2"); // "2" indicates that the user name is available.} Out. flush (); out. close () ;}catch (Exception e) {e. printStackTrace (); response. setStatus (405 );}}}
Code 5web. xml file:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <servlet> <servlet-name>AjaxCheckUserNameServlet</servlet-name> <servlet-class>com.ghj.packagofserlet.AjaxCheckUserNameServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AjaxCheckUserNameServlet</servlet-name> <url-pattern>/AjaxCheckUserNameServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
The above is the detailed code that uses the traditional method to Implement Asynchronous verification. I hope it will be helpful for your learning.
Articles you may be interested in:
- Javascript changes the asynchronous validation form to a synchronous form