코드를 직접 업로드하십시오.
reg_ajax.html
<! doctype html> <html> <head> <meta charset = "utf-8"> <title> ajax는 사용자 이름이 존재하는지 확인하기 위해 사용자 이름을 구현하도록 서브 렛을 요청합니다 </title> </head> <body> <script type = "text/javaScript">/*** get xmlhtprequest*/function getajax ques xmlhttp; 시도 {// firefox, Opera 8.0+, Safari xmlhttp = new xmlhttprequest (); } catch (e) {// Internet Explorer try {xmlhttp = new ActiveXobject ( "msxml2.xmlhttp"); } catch (e) {try {xmlhttp = new ActiveXobject ( "microsoft.xmlhttp"); } catch (e) {alert ( "브라우저는 Ajax를 지원하지 않습니다!"); 거짓을 반환합니다. }}} return xmlhttp; }/** * ajax 요청 * URL-Quarest URL을 서버로 request URL을 요청합니다 * MethodType (post/get) * con (true (Asynchronous) | false (synchronous)) * functionName (콜백 메소드 이름, 인용 표시가 필요하지 않아야합니다. ajaxRequest (url, methodtype, con, functionName) {// get xmlhttprequest 객체 var xmlhttp = getajaxhttp (); // 콜백 함수를 설정 (응답 할 때 호출 된 함수) xmlhttp.onreadyStateChange = function () {// xmlhttprequest 객체에서 호출 된이 함수의 코드는 언제입니까? // 서버가 응답하면 xmlhttprequest 객체는 (xmlhttp.readystate == 4) {if (xmlhttp.status == 200) {functionName (xmlhttp.responsetext); }}}}; // 요청 생성 xmlhttp.open (methodtype, url, con); // 요청을 요청 xmlhttp.send (); } function CheckUserName () {var username = document.getElementById ( 'username'). 값; // ajax 요청 servlet ajaxrequest 만들기 ( "userervlet? username ="+username, "post", true, ckusernameresponse); } function CKUSERNAMERESPONSE (responseContents) {if (responseContents == 'yes') {document.getElementById ( 'usernamemsg'). innerHtml = "<font color = 'red'> username Evential </font>"; document.getElementById ( 'username'). style = "background-color : red"; } else {document.getElementById ( 'usernamemsg'). innerhtml = ""; document.getElementById ( 'username'). style = "background-color : white"; }} </script> <table> <td> <td> username </td> <td> <input type = "text"id = "username"onblur = "checkusername ()" "/> </td> <td> <div id ="usernamemsg "> </ddd> <td> <td> <td> type = "text"id = "이메일"/> </td> <td> <div id = "emailmsg"> </div> </td> </tr> </table> </body> </html>요청 된 서블릿 코드는 다음과 같습니다. Userservlet.java
패키지 CN.Bestchance.servlet; import java.io.ioexception; import java.util.arraylist; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpervlet; import javax.servlet.servlet javax.servlet.http.http.httpservletrequest; import javax.servlet.http.htttp.httpservletresponse;/** * servlet 구현 클래스 userservlet */@webservlet ( "/userservlet") public userservlet httpservlet {private static final long serialversionuid = 1l; 보호 된 void doget (httpservletRequest 요청, httpservletResponse 응답) servletexception, ioexception {dopost (요청, 응답); } / ** * @ @httpservlet#dopost (httpservletrequest request, httpservletresponse response) * / protected void dopost (httpservletrequest request, httpservletrepesponse 응답) servletexception, ioexception {string username = request.getername "); // ArrayList는 데이터베이스에서 데이터를 쿼리하는 대신 여기에서 사용됩니다. ArrayList <string> userList = New ArrayList <string> (); userList.Add ( "admin"); userList.Add ( "테스트"); userList.Add ( "기회"); // 사용자 이름에 부울 플래그가 있는지 여부를 확인하십시오. for (문자열 문자열 : userList) {if (string.equals (username)) {flag = true; 부서지다; }} if (flag) {// 사용자 이름이 이미 존재합니다. } else {// 사용자 이름이 존재하지 않습니다. replay.getWriter (). print ( "no"); }}}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.