必要
ユーザーが入力したアカウントパスワードがAjaxの非同期更新ページを介してデータベースに存在するかどうかを確認します。
テクノロジースタック
JSP+サーブレット+オラクル
特定のコード
JSPセクション:
<%@ page Language = "Java" contentType = "text/html; charset = utf-8" pageencoding = "utf-8"%> < "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv =" content-type "content =" text/html; charset = utf-8 "> <title>タイトル</title> </head </head> <script> <script> <cript> <creati xmlhttp = new xmlhttprequest(); // IE以外のその他のブラウザはajax} catch(tryms){xmlhttp = new ActiveXObject( "msxml2.xmlhttp"); ActiveXObject( "microsoft.xmlhttp"); // ie browser adaptation} catch(failed){xmlhttp = null; }} xmlhttpを返します。 } //リクエストを送信するvar xmlhttp; function checkuseRexists(){var u = document.getElementById( "uname"); var username = u.value; if(username == ""){alert( "username"を入力してください "); u.focus(); falseを返します。 } //文字列var url = "loginservlet"; // core xmlhttprequestコンポーネントxmlhttp = createxmlhttprequest(); //コールバック関数を設定xmlhttp.onreadystatechange = proessRequest; // core xmlhttp.open( "post"、url、true)を初期化します。 //リクエストヘッダーxmlhttp.setRequestheader( "content-type"、 "application/x-www-form-urlencoded;"); //リクエストを送信xmlhttp.send( "uname ="+username); } // callback関数関数burdenrequest(){if(xmlhttp.status == 200 && xmlhttp.readystate == 4){var b = xmlhttp.responsetext; //サーバーの出力結果を取得color = 'red'>ユーザー名は既に存在します</font> "; } else {document.getElementById( "alert")。innerhtml = "<font color = 'blue'>ユーザー名を使用できます!</font>"; }}}} </script> <body> username:<input id = "uname" name = "uname" type = "text" onblur = "checkuserexists()"/> <div id = "alert" style = "display:inline"> </div> </body> </html> </html>ここにはDAO層はありません。検証のためにサーブレットとサービスレイヤーを使用するだけです。
以下は、サービス中のJDBCクエリのコードです。
java.sql.connection; import java.sql.resultset; import java.sql.sqlecception; Import java.sql.Statement; Import com.stx.service.connectionmanager; public searchuser(public boolean searchuser(fi us us){ /jaxser)名前はboolean isfalse = false;接続接続= null;ステートメントstmt = null;結果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); } isfalseを返します。 }}JDBC接続コード:
java.sql.connection; Import java.sql.drivermanager; Import java.sql.Resultset; Import java.sql.sql.Statement; import java.sql.Statement; public connectionmanager {private final static string driver_class = "oracle.jdbc.oracledriver"; private final static string url = "jdbc:oracle:thin:@localhost:1521:orcl";プライベート最終静的文字列dbname = "ibook";プライベート最終的な静的文字列パスワード= "qwer"; public static Connection getConnection(){connection connection = null; try {class.forname(driver_class); connection = drivermanager.getConnection(url、dbname、password); } 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 cordestatement(statement stmt){try {if(stmt!= null)stmt.close(); } catch(sqlexception e){e.printstacktrace(); }}}ユーザークラスについて:
パブリッククラスユーザー{プライベート文字列uname; public user(){super(); } public user(string uname){super(); this.uname = uname; } public string getuname(){return uname; } public void setuname(string uname){this.uname = uname; }コントロールレイヤーサーブレットについて:
java.io.ioexception; Import java.io.printwriter; import javax.servlet.servletexception; Import javax.servlet.http.httpservlet; Import javax.servlet.http.httpservletrequest; Import Javax.http.httpseponseponse; com.stx.service.ajaxservice;/** *サーブレット実装クラスloginservlet */public class loginservlet extends httpservlet {private static final long serialversionuid = 1l; private ajaxservice ajaxservice = new ajaxservice(); / ** * @see httpservlet#httpservlet() */ public loginservlet(){super(); // TODO自動生成コンストラクタースタブ}/ ** * @see httpservlet#doget(httpservletrequest request、httpservletResponse応答) string uname = request.getParameter( "uname"); //入力ユーザー名Boolean isuname = ajaxservice.searchuser(uname); //サービスresponse.setcharacterencoding( "utf-8"); out.print(isuname); out.flush(); out.close(// close resources}/** * @see httpservlet#dopost(httpservletrequest request、httpservletresponse応答) }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。