필요
사용자가 입력 한 계정 암호가 AJAX 비동기식 새로 고침 페이지를 통해 데이터베이스에 존재하는지 확인하십시오.
기술 스택
JSP+Servlet+Oracle
특정 코드
JSP 섹션 :
<%@ page language = "java"contmenttype = "text/html; charset = utf-8"pageencoding = "utf-8"%> <! doctype html public "-// w3c // dtml 4.01 Transitional // en" "http://www.w3.org/tr/tr/html4/loose.dtd"> html> html> http-quiv ="content-type "content ="text/html; charset = utf-8 "> <title> 제목 </head> <script> 함수 createxmmlhtprequest <) xmlhttp = new xmlhttprequest (); // ie ajax} catch (tryms) 이외의 다른 브라우저 {try {xmlhttp = new ActiveXobject ( "msxml2.xmlhttp"); // iE 브라우저 적응} catch (try {xmlhtp) {xmlhtp = new ActiveXobject ( "microsoft.xmlhttp"); // 브라우저 적응} catch (실패) {xmlhttp = null; }} return xmlhttp; } // 요청 제출 var xmlhttp; function CheckuserExists () {var u = document.getElementById ( "unam"); var username = u.value; if (username == "") {alert ( "사용자 이름을 입력하십시오"); U.focus (); 거짓을 반환합니다. } // 문자열 var url = "loginservlet"에 액세스하십시오. // 코어 생성 xmlhttprequest 구성 요소 xmlhttp = createxmlhttprequest (); // 콜백 함수를 설정 XMLHTTP.ONREADYSTATECHANGE = proEssRequest; // 핵심 초기화 xmlhttp.open ( "post", url, true); // 요청 헤더 xmlhttp.setRequestHeader ( "content-type", "application/x-www-form-urlencoded;"); // 요청을 보내 xmlhttp.send ( "uname ="+username); } // 콜백 함수 함수 burdenRequest () {if (xmlhttp.status == 200 && xmlhttp.readystate == 4) {var b = xmlhttp.responsetext; // if (b == "tree") {document.getElementByid ( "alert"). color = 'red'> 사용자 이름이 이미 존재합니다! } else {document.getElementById ( "Alert"). innerHtml = "<font color = 'blue'> 사용자 이름을 사용할 수 있습니다! </font>"; }}}}} </script> <body> 사용자 이름을 입력하십시오 : <input id = "uname"name = "uname"type = "text"onblur = "Checkuserexists ()"/> <div id = "alert"style = "display : inline"> </div> </body> </html> 여기에는 DAO 계층이 없으며 검증을 위해 서블릿과 서비스 계층을 사용하십시오.
다음은 서비스중인 JDBC 쿼리 코드입니다.
import java.sql.connection; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import com.stx.service.user; import com.stx.service.connectionmanager; public class ajaxservice {public ajaxservice {// jd) {// jd) 이름은 부울 isfalse = false; 연결 연결 = null; 문자 stmt = null; resultSet rs = null; Connection = ConnectionManager.getConnection (); try {stmt = connection.createstatement (); 문자열 sql = "select * from user_b where uname = '+uname+"' "; // sql 문 rs = stmt.executeQuery (sql); isfalse = rs.next (); } catch (sqlexception e) {e.printstacktrace (); } 마침내 {ConnectionManager.closeresultset (rs); ConnectionManager.closestatement (STMT); ConnectionManager.closeConnection (Connection); } return isfalse; }}JDBC 연결 코드 :
Java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; public connectionManager {private final static string driver_class = "oracle.jdbc.oracdriver"; 개인 최종 정적 문자열 URL = "JDBC : Oracle : Thin : @LocalHost : 1521 : orcl"; 개인 최종 정적 문자열 dbname = "iBook"; 개인 최종 정적 문자열 암호 = "QWER"; 공개 정적 연결 getConnection () {Connection Connection = null; try {class.forname (driver_class); Connection = DriverManager.getConnection (URL, DBNAME, PASSFARPT); } catch (classNotFoundException e) {e.printstacktrace (); } catch (sqlexception e) {e.printstacktrace (); } 반환 연결; } public static void closeResultset (resultSet rs) {try {if (rs! = null) rs.close (); } catch (sqlexception e) {e.printstacktrace (); }} public static void closeConnection (Connection Connection) {try {if (connection! = null &&! connection.isclosed ()) connection.close (); } catch (sqlexception e) {e.printstacktrace (); }} public static void closestatement (statement stmt) {try {if (stmt! = null) stmt.close (); } catch (sqlexception e) {e.printstacktrace (); }}}사용자 클래스 정보 :
공개 클래스 사용자 {개인 문자열 uname; public user () {super (); } 공개 사용자 (String Uname) {super (); this.uname = uname; } public String getUname () {return uname; } public void setuname (String Uname) {this.uname = uname; }제어 레이어 서블릿에 대해 :
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.httpervletrrestronge; com.stx.service.ajaxservice;/** * 서블릿 구현 클래스 loginservlet */public class loginservlet 확장 httpservlet {private static final long serialversionuid = 1l; 개인 ajaxservice ajaxservice = new ajaxservice (); / ** * @httpservlet#httpservlet () */ public loginservlet () {super (); // TODO 자동 생성 생성자 스텁}/ ** * @ @httpservlet#doget (httpservletrequest request, httpservletreponse responsk) */ protected void doget (httpservletrequest 요청, httpservletreponse 응답) servletexception, ioexception, ioexception request.setcharacterencoding ( "UTF-8"); String uname = request.getParameter ( "Uname"); // 입력 사용자 이름을 얻습니다. boolean isuname = ajaxservice.searchuser (uname); // 서비스 응답에서 쿼리 메소드 호출 .setcharacterencoding ( "utf-8"); // 문자 인코 인자 인쇄기 세트. out.print (isuname); out.flush (); out.close (); // close resources}/** * @see httpservlet#dopost (httpservletrequest 요청, httpservletreponse responsk) */protected void dopost (httpservletrequest request), httpservletrepsone 응답) servletexception, ioxception {// auto-geted methether to a auto-geted methodception, httpservletrequest 요청) 응답); }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.