1<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2pageencoding= "UTF-8"%>3<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >456<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">7<title>insert title here</title>89<script type= "Text/javascript" >Ten var req; One function Check () A { -var url = "Checkuser?uname=" + document.getElementById ("uname"). Value; - if(window. XMLHttpRequest) the { -req =NewXMLHttpRequest (); - } - Else if(window. ActiveXObject) { +req =NewActiveXObject ("Microsoft.XMLHTTP"); - } +Req.open ("GET", URL,true); AReq.onreadystatechange =Show; atReq.send (NULL); -Alert ("HHHH"); - } - function Show () { - if(Req.readystate = = 4) { - if(Req.status = = 200) { indocument.getElementById ("Info"). innerhtml=Req.responsetext; - } to } + } -</script> the<body> *<table> $<tr><td> username </td><td><input type= "text" id= "uname" ></td></tr>Panax Notoginseng<tr><td><input type= "button" onclick= "Check ()" value= "Detect user" ></td><td><div id= " Info "></div> </td></tr> -</table> the</body> +1 PackageJavastudy;2 3 Importjava.io.IOException;4 Importjavax.servlet.ServletException;5 ImportJavax.servlet.annotation.WebServlet;6 ImportJavax.servlet.http.HttpServlet;7 Importjavax.servlet.http.HttpServletRequest;8 ImportJavax.servlet.http.HttpServletResponse;9 Ten /** One * Servlet Implementation class CheckUser A */ -@WebServlet ("/checkuser") - Public classCheckUserextendsHttpServlet { the Private Static Final LongSerialversionuid = 1L; - - /** - * @seeHttpservlet#httpservlet () + */ - PublicCheckUser () { + Super(); A //TODO auto-generated Constructor stub at } - - /** - * @seeHttpservlet#doget (httpservletrequest request, httpservletresponse response) - */ - protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { in //TODO auto-generated Method Stub -String uname=request.getparameter ("uname"); toString[] names={"Zhang", "Yang", "Wang"}; + Booleanflag=true; - for(intI =0;i<names.length;i++) the { * if(Names[i].equals (uname)) $ {Panax Notoginsengflag=false; - Break; the } + A } theResponse.setcharacterencoding ("Utf-8"); + if(flag==false) - { $Response.getwriter (). println ("The user name is registered!!!"); $ } - Else - { theResponse.getwriter (). println ("Registered success!!! "); - }Wuyi //response.getwriter (). Append ("Served at:"). Append (Request.getcontextpath ()); the } - Wu /** - * @seeHttpservlet#dopost (httpservletrequest request, httpservletresponse response) About */ $ protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { - //TODO auto-generated Method Stub - doget (request, response); - } A +}
Using AJAX to implement user name detection (JavaScript method)