Using traditional methods in Web pages to implement asynchronous validation explanations

Source: Internet
Author: User

Learning JavaScript asynchronous checksum is often from the most traditional XMLHttpRequest, today Saturday, I will talk about the traditional calibration of the understanding:

Code 1--index.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 2--demo1.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 3--demo2.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 4--ajaxcheckusernameservlet.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;/** * Processing D  Asynchronous Validation in emo1.jsp * * @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);//intentional exception to check demo1.jsp xmlhttprequest.status!= Whether the 200 branch statement is available in string Username=request.getparameter ("UserName");//Gets the "username" System.out.println (" Handles asynchronous authentication in demo1.jsp with the user name: "+username", if ("admin". Equals (UserName)) {Out.print ("1"), or "1" indicating 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);}} /** * Processing Asynchronous validation in demo2.jsp * * @author Gaohuanjie */public void DoPost (HttpServletRequest request, HttpServletResponse Respon SE) throws Servletexception, IOException {try{response.setcharacterencoding ("UTF-8"); Request.setcharacterencoding ( "UTF-8"); PrintWriter out = Response.getwriter ();//system.out.println (1/0);//intentional exception to check demo2.jsp xmlhttprequest.status!= Whether the 200 branch statement is available in string Username=request.getparameter ("UserName");//Gets the "username" System.out.println (" Handles asynchronous authentication in demo2.jsp with the user name: "+username", if ("admin". Equals (UserName)) {Out.print ("1"), or "1" indicating 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 5--web.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>
0 min Download Resources

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.