この記事では、ユーザーログイン、登録、ログアウトなどのユーザーログインを実現するためにJavabeanの使用について簡単に説明します。
1。ジャバビアンについて
Javabeanは、Java言語で書かれた再利用可能なコンポーネントです。 Javabeanとして書かれるには、クラスは具体的かつ公開され、パラメーターのないコンストラクターを備えている必要があります。 Javabeansは、内部ドメインをメンバープロパティに公開し、一貫した設計パターンに準拠するパブリックメソッドを提供することにより、メソッドを設定および取得します。誰もが知っているように、属性名はこのパターンに準拠しており、他のJavaクラスは、内省メカニズムを通じてこれらのJavabeansの特性を発見し、操作できます。
2。システムアーキテクチャ
2.1ログインユースケース図
2.2ページフローチャート
2.3システムアーキテクチャ図
2.4データベース設計
この例では、Oracleデータベースを使用しています
ユーザーテーブルには、ID、ユーザー名、パスワード、電子メール、合計4つのフィールドが含まれています。
- テーブルの作成テーブルの作成p_user(id varchar2(50)not not null、username varchar2(20)、password varchar2(20)、email varchar2(50))テーブルスペースユーザーpctfree 10 initrans 1 maxtrans 255ストレージ(初期64点伸び1最大Xextents Unlimited); - テーブルP_Userのコメントにコメントを追加します。 -columns p_user.idの列にコメントを追加する列にコメントを追加します。列p_user.usernameへのコメントは「username」です。列p_user.passwordへのコメントは「パスワード」です。列p_user.emailへのコメントは「電子メール」です。
3。Javabean執筆
3.1 Javabeansの基礎となるデータベース処理の開発
dbacess.java
パッケージcom.baosight.bean; Java.sql.Connectionをインポートします。 Java.sql.drivermanagerをインポートします。 java.sql.resultsetをインポートします。 java.sql.sqlexceptionをインポートします。 java.sql.Statementをインポートします。 /**データベース操作クラス * <p>タイトル:dbacess </p> * <p>説明:todo </p> * <p> company:</p> * @author yuan * @date 2016-5-22 12:40:24 */public class dbacess {private string driver = "oracle.jdbc.driver.oracledriver"; private string url = "jdbc:oracle:" + "thin:@127.0.0.1:1521:orcl";プライベート文字列username = "scott";プライベート文字列パスワード= "Tiger";プライベート接続conn;プライベートステートメントSTM;プライベート結果rs; //接続の作成public boolean createconn(){boolean b = false; try {class.forname(driver); // Oracle Driver conn = drivermanager.getConnection(url、username、password); b = true; } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); } //接続キャッチを取得(classNotFoundException e){// todo auto-enerated catch block e.printstacktrace(); } burten b; } //パブリックブールアップデート(String SQL){boolean b = false; try {stm = conn.createStatement(); stm.execute(sql); b = true; } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); } burten b; } // query public void query(string sql){try {stm = conn.createstatement(); rs = stm.executequery(sql); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }} //データがあるかどうかを判断public boolean next(){boolean b = false; try {if(rs.next()){b = true; }} catch(sqlexception e){// todo auto-feenated catch block e.printstacktrace(); } burten b; } //テーブルフィールド値を取得しますpublic string getValue(string field){string value = null; try {if(rs!= null){value = rs.getString(field); }} catch(sqlexception e){// todo auto-feenated catch block e.printstacktrace(); } return値; } //接続を閉じるpublic public void closeconn(){try {if(conn!= null){conn.close(); }} catch(sqlexception e){// todo auto-feenated catch block e.printstacktrace(); }} // closeステートメントpublic void coldestm(){try {if(stm!= null){stm.close(); }} catch(sqlexception e){// todo auto-feenated catch block e.printstacktrace(); }} // close resultSet public void closers(){try {if(rs!= null){rs.Close(); }} catch(sqlexception e){// todo auto-feenated catch block e.printstacktrace(); }} public string getDriver(){return driver; } public void setDriver(string driver){this.driver = driver; } public string geturl(){return url; } public void seturl(string url){this.url = url; } public string getUsername(){return username; } public void setUsername(string username){this.username = username; } public string getPassWord(){パスワードを返します。 } public void setPassword(string password){this.password = password; }公式声明getStm(){return stm; } public void setStm(statement stm){this.stm = stm; } public resultSet getrs(){return rs; } public void setrs(resultset rs){this.rs = rs; } public void setConn(connection conn){this.conn = conn; } public Connection getConn(){return conn; }}上記のデータベース操作クラスでは、JDBCを使用してデータベースに接続し、データベースへの接続、クエリ、変更、およびクロージングリソースなどのメソッドをカプセル化します。
3.2 Javabeanビジネスロジックコンポーネントを開発します
userbean.java
パッケージcom.baosight.bean; com.baosight.util.generateuuidをインポートします。 /** * <p>タイトル:userbean </p> * <p>説明:todo </p> * <p> company:</p> * @author yuan * @date 2016-5-22 1:05:00 pm */public class userbean {//ログイン検証public boolean valid(sring username、string userid = false; dbacess db = new dbacess(); if(db.createconn()){string sql = "select * from p_user where username = '"+username+"'およびpassword = '"+password+"'"; DB.Query(SQL); if(db.next()){isvalid = true; } db.closers(); db.closestm(); db.closeconn(); } isvalidを返します。 } //登録検証public boolean isexist(string username){boolean isvalid = false; dbacess db = new dbacess(); if(db.createconn()){string sql = "select * from p_user where username = '"+username+"'"; DB.Query(SQL); if(db.next()){isvalid = true; } db.closers(); db.closestm(); db.closeconn(); } isvalidを返します。 } //登録ユーザーpublic boolean add(string username、string password、string email){boolean isvalid = false; dbacess db = new dbacess(); if(db.createconn()){string sql = "p_user(id、username、password、email)values( '"+generateuuid.next()+"'、 '"+username+"'、 '"+"' '、'"+email+"'); isvalid = db.update(sql); db.closeptm(); db.closeconn();上記のビジネスロジックJavabeanは、ログイン検証、登録確認、新規ユーザーなどの方法を定義しています
3.3一意のIDの生成について
上記は、新しいユーザーを追加するときにIDを挿入する必要があります。この例では、UUIDは一意のIDを生成するために使用されます。
GenerateUuid.java
パッケージcom.baosight.util; Import Java.util.date; /** * <p>タイトル:GenerateUuid </p> * <p>説明:todo </p> * <p> company:</p> * @author yuan * @date 2016-5-22 1:31:46 pm */public class generateuid {private static date = new date(); // private static stringbuilder buf = new StringBuilder(); private static int seq = 0;プライベート静的最終intローテーション= 9999; public static同期long next(){if(seq> rotation)seq = 0; // buf.delete(0、buf.length()); date.settime(system.currenttimemillis()); string str = string.format( "%1 $ ty%1 $ tm%1 $ td%1 $ tk%1 $ tm%1 $ ts%2 $ 05d"、日付、seq ++); long.parselong(str); } public static void main(string [] args){for(int i = 0; i <100; i ++){system.out.println(next()); }}} 4。ページライティング
4.1ログインページ
login.jsp
<%@ page Language = "java" import = "java.util。*" pageencoding = "utf-8"%> <%string path = request.getContextPath(); string basepath = request.getscheme()+"://"+request.getServername()+":"+request.getServerport()+path+"/"; %> <!doctype html public " - // w3c // dtd html 4.01 transitional // en"> <html> <head> <base href = "<%= basepath%>"> <title> loginページcontent = "no-cache"> <meta http-equiv = "compers" content = "0"> <meta http-equiv = "keywords" content = "keyword1、keyword2、keyword3"> <meta http-equiv = "description" content = "this is my page"> <! href = "styles.css"> - > </head> <body> <form action = "login_action.jsp" method = "post"> <table> <tr> <td colspan = "2"> login window </td> </tr> <tr <td> username:</td> <td> </td </td </td> </td> </td> </td> <td>パスワード:</td> <td> <入力タイプ= "text" name = "password"/> </td> </tr> <tr> <td colspan = "2"> <input type = "submit" value = "login"/> <a href = "register.jsp">レジスタ</a>
ページ効果
4.2ビジネスロジック処理ページにログインします
login_action.jsp
<%@ page Language = "Java" Import = "Java.util。*" pageEncoding = "utf-8"%> <%@ page import = "java.sql。 string basepath = request.getscheme()+"://"+request.getServername()+":"+request.getServerport()+path+"/"; %> <%string username = request.getParameter( "username");文字列パスワード= request.getParameter( "password"); if(username == null || ""。equals(username.trim())|| password == null || ""。equals(password.trim())){//out.write( "usernameまたはpasswordは空にすることはできません!"); System.out.println( "ユーザー名またはパスワードは空にすることはできません!"); Response.sendredirect( "login.jsp");戻る; //REQUEST.GETREQUESTDISPATCHER("login.jsp").Forward(REQUEST、Response); } userbean userbean = new userbean(); boolean isvalid = userbean.valid(username、password); if(isvalid){system.out.println( "ログインに正常に!"); session.setattribute( "username"、username); Response.sendredirect( "welcome.jsp");戻る; } else {system.out.println( "不明なユーザー名またはパスワード、ログインに失敗!"); Response.sendredirect( "login.jsp");戻る; }%>上記のJSPは、ビジネス処理のためにJavabeanを呼び出します。ユーザー名またはパスワードが空のときにログインページに戻ります。
ログインが成功したら、ユーザー情報をセッションに保存して、ウェルカムページwelcome.jspにジャンプします
login.jspが失敗したときにログインページに戻ります
4.3ログインページを正常に歓迎します
welcome.jsp
<%@ page Language = "java" import = "java.util。*" pageencoding = "utf-8"%> <%string path = request.getContextPath(); string basepath = request.getscheme()+"://"+request.getServername()+":"+request.getServerport()+path+"/"; %> <!doctype html public " - // w3c // dtd html 4.01 transitional // en"> <html> <head> <base href = "<%= basepath%>"> <title> my jsp 'welcom.jsp'スタートページ</title> <meta http-equiv = "pragma" http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "content =" 0 "> <meta http-equiv =" keyword1、keyword3 "keyword3"> <meta http-equiv = "content =" type = "text/css" href = "styles.css"> </head> <body> <table> <tr> <tr> <td> <img src = "images/logo4.png"/> </td> <td> <img src = "images/logo2.png colspan = "2"> <hr/> </td> </tr> <tr> <td> <table> <tr> <td> <a> main </a> </td> </tr> <tr> <td> <a> menu1 <td> <a> menu3 </a> </td> </tr> <tr> <td> <a> menu4 </a> </td> </tr> <tr> <td> <a> menu5 </a> </td> </tr> <tr> <td> <a> menu> menu> </td> </td </</td> </ < </tr> <tr> <td> <a> menu7 </a> </td> </tr> <tr> <td> <a> menu> menu8 </a> </td> </tr> </tr </tr> </tr> </tr> </tr> </tr> </tr <td> <form Action = <tD colspan = "2">正常にログイン!</td> </tr> <tr> <td>ようこそ、</td> <td> $ {username} </td> </tr> <tr> <td>歓迎、</td> <tr> $ {username} </td> value = "exit"/> </td> </tr> </table> </form> </td> </tr> </tr </table> </body> </html>ページ効果
4.4ビジネス処理ページからログアウトします
loginout.jsp
<%@ page Language = "java" import = "java.util。*" pageencoding = "utf-8"%> <%string path = request.getContextPath(); string basepath = request.getscheme()+"://"+request.getServername()+":"+request.getServerport()+path+"/"; %> <!doctype html public " - // w3c // dtd html 4.01 transitional // en"> <html> <head> <base href = "<%= basepath%>"> <title> my jsp 'loginout.jsp'スタートページ</タイトル> http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "content =" 0 "> <meta http-equiv =" keyword1、keyword3 "keyword3"> <meta http-equiv = "content =" type = "text/css" href = "styles.css"> - > </head> <body> <%session.removeattribute( "username"); Response.sendredirect( "login.jsp"); %> </body> </html>
セッションからユーザー情報を削除し、ログインページlogin.jspにジャンプします
4.5ユーザー登録ページ
Register.jsp
<%@ page Language = "java" import = "java.util。*" pageencoding = "utf-8"%> <%string path = request.getContextPath(); string basepath = request.getscheme()+"://"+request.getServername()+":"+request.getServerport()+path+"/"; %> <!doctype html public " - // w3c // dtd html 4.01 transitional // en"> <html> <head> <base href = "<%= basepath%>"> <title>登録ページcontent = "no-cache"> <meta http-equiv = "compers" content = "0"> <meta http-equiv = "keywords" content = "keyword1、keyword2、keyword3"> <meta http-equiv = "description" content = "this is my page"> <! href = "styles.css"> - > </head> <body> <form action = "register_action.jsp" "post"> <table> <tr> <td colspan = "2"> register window </td> </tr <tr <td> username:</td> <td> <input = "" username "/</td> </td> < <td>パスワード:</td> <td> <入力タイプ= "テキスト" name = "password1"/> </td> </td> </tr> <tr> <td>パスワードの確認:</td> <td> <入力タイプ= "Text" name = "password2"/> </td> </tr> <td> email:</td <td <td <td <td <"" email "" "" "" omm /> </td> </tr> <tr> <td colspan = "2"> <inputタイプ= "submit" value = "register"/> <a href = "login.jsp"> return </a> </td> </tr </table> </form> </body> </html>
ランニング効果
4.6登録事業処理ページ
<%@ page Language = "Java" Import = "Java.util。*" pageEncoding = "utf-8"%> <%@ page import = "java.sql。 string basepath = request.getscheme()+"://"+request.getServername()+":"+request.getServerport()+path+"/"; %> <%string username = request.getParameter( "username");文字列password1 = request.getParameter( "password1");文字列password2 = request.getParameter( "password2");文字列email = request.getParameter( "email"); if(username == null || ""。equals(username.trim())|| password1 == null || ""。equals(password1.trim())|| password2 == null || ""。 System.out.println( "ユーザー名またはパスワードは空にすることはできません!"); Response.sendredirect( "Register.jsp");戻る; //REQUEST.GETREQUESTDISPATCHER("login.jsp").Forward(REQUEST、Response); } userbean userbean = new userbean(); boolean isexit = userbean.isexist(username); if(!isexit){userbean.add(username、password1、email); system.out.println( "登録された登録、ログインしてください!"); Response.sendredirect( "login.jsp");戻る; } else {system.out.println( "ユーザー名既に存在します!"); Response.sendredirect( "Register.jsp");戻る; }%>上記のJSPは、ビジネス処理のためにJavabeanを呼び出します
ユーザー名またはパスワードが空の場合に登録ページに戻ります登録jsp
登録されたユーザー名がデータベースに存在しない場合、新しいユーザーが追加されます
新しい追加が成功したら、ログインページlogin.jspにジャンプします
登録済みのユーザー名がデータベースに存在する場合、登録ページ登録ページに戻りますjsp
5。概要
この例では、Javabeanを使用して、データベース操作とビジネスロジック処理をカプセル化します。
上記は、Javabeanを使用してユーザーログインを実現するための簡単な紹介であり、継続的に改善する必要があります。誰もが一緒に学び、進歩を遂げることができることを願っています!