A detailed analysis of JavaScript traditional methods to implement asynchronous verification _javascript skills

Source: Internet
Author: User
Tags flush

Learning JavaScript Asynchronous checksums is often learned from the most traditional xmlhttprequest, and this article discusses the understanding of traditional checksums:
Code 1index.jsp files:

<%@ 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 = 638774497676 
 
  5210524L; /** * Processing demo1.jsp Asynchronous Validation * * @author Gaohuanjie/public void doget (HttpServletRequest request, Httpserv 
      Letresponse response) throws Servletexception, IOException {try{response.setcharacterencoding ("UTF-8"); 
      Request.setcharacterencoding ("UTF-8"); 
      PrintWriter out = Response.getwriter (); System.out.println (1/0);//intentionally unexpected exception to check whether the branch statement xmlhttprequest.status!=200 in Demo1.jsp is available with String username= 
      Request.getparameter ("UserName")//Get "username" System.out.println ("Processing demo1.jsp asynchronous authentication, Username:" +username); if ("admin". Equals (UserName)) {
        Out.print ("1");//"1" indicates that the user name is not available. 
      else {out.print ("2");//"2" indicates that the user name is available. 
      } out.flush (); 
    Out.close (); 
      }catch (Exception e) {e.printstacktrace (); 
    Response.setstatus (405); demo2.jsp Asynchronous Validation * * @author Gaohuanjie/public void DoPost (HttpServletRequest re)/** * processing Quest, HttpServletResponse response) throws Servletexception, IOException {try{response.setcharacterencoding ( 
      "UTF-8"); 
      Request.setcharacterencoding ("UTF-8"); 
      PrintWriter out = Response.getwriter (); System.out.println (1/0);//intentionally unexpected exception to check whether the branch statement xmlhttprequest.status!=200 in Demo2.jsp is available with String username= 
      Request.getparameter ("UserName")//Get "username" System.out.println ("Processing demo2.jsp asynchronous authentication, Username:" +username); 
      if ("admin". Equals (UserName)) {Out.print ("1");//"1" indicates that the user name is not available. 
      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> 

The above is the use of traditional methods to implement the detailed code of asynchronous checksum, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.