jquery is really a good JavaScript framework, and today we use our spare time to give you an example of the uniqueness of using jquery asynchronous implementation to verify the user name:
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--jqueryajaxcheckusernameservlet.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 Jqueryajaxcheckusernameservlet extends HttpServlet {private static final long serialversionuid = 6387744976765210524L; public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { DoPost (request,response);} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {try{ Response.setcharacterencoding ("UTF-8"); Request.setcharacterencoding ("UTF-8"); System.out.println (1/0);//intentional exception to check index.jsp if the error method is available printwriter out = Response.getwriter (); String username=request.getparameter ("UserName");//Gets the "user name" if ("admin". Equals (UserName)) {out.write ("{\" flag\ ": \" False\ "}");//"false" indicates that the user name is not available. } else {out.write ("{\" flag\ ": \" True\ "}");//"TruE "indicates that the user name is available. }out.flush (); Out.close ();} catch (Exception e) {e.printstacktrace (); Response.setstatus (405);//The error method in index.jsp is executed at this time. }}}
Code 3--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> Jqueryajaxcheckusernameservlet</servlet-name> <servlet-class> Com.ghj.packagofserlet.jqueryajaxcheckusernameservlet</servlet-class> </servlet> <servlet-mapping > <servlet-name>JqueryAjaxCheckUserNameServlet</servlet-name> <url-pattern>/ Jqueryajaxcheckusernameservlet</url-pattern> </servlet-mapping> <welcome-file-list> < Welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
Note: The above example uses the Jquery-1.8.3.min.js file, which can be found in the download resources below."0-minute download resources "
" Asynchronous submission of jquery forms using Loadmask "