この記事では、主にすべての人のための本モールのユーザーモジュールを分析します。特定のコンテンツは次のとおりです。
1。ユーザーモジュールの関連クラス作成
ドメイン:ユーザー
DAO:userdao
サービス:userdao
web.servlet:userservlet
2。ユーザー登録
2.1登録プロセス
/jsps/user/regist.jsp-> userservlet#regist() - > msg.jsp
2.2登録ページ
<%@ページ言語= "java" import = "java.util。*" pageencoding = "utf-8"%> <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%> < Transitional // en "> <html> <head> <title> Register </title> <meta http-equiv =" pragma "content =" no-cache "> <meta http-equiv =" cache-control "content =" no-cache "> <meta http-equiv =" content = " content = "keyword1、keyword2、keyword3"> <meta http-equiv = "descripting" content = "これは私のページ"> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"> <! - <link rel = "styletheet" Type = "href =" fide " <body> <h1>登録</h1> <% - 1。Showエラー - >フィールドエラー2。例外エラーを表示3。Echo - %> <P style = "Color:Red; font-weight:900"> $ {msg} </p> <form action = "<c:url value = '/useservlet'/> username:<input type = "text" name = "username" value = "$ {form.username}"/> <span style = "color:red; font-weight:900"> $ {errors.username} </span> <br/>パスワード:パスワード:<入力タイプ= "パスワード" name = "passward" $ {color = "/< font-weight:900 "> $ {errors.password} </span> <br/>電子メール:<入力タイプ="テキスト "name =" email "value =" $ {form.email} "/> <span style =" color:red; font-weight:900 "> $ {errors.email} </span> <br/> <入力タイプ=" value = "register"/> </form> </body> </html>2.3ユーザーサーブレット
ユーザー
/**ユーザーのドメインオブジェクト*/パブリッククラスユーザー{/**対応するデータベーステーブル*/プライベート文字列UID; //プライマリキープライベート文字列ユーザー名; //ユーザー名プライベート文字列パスワード; //パスワードプライベート文字列メール; //電子メールプライベート文字列コード;ベースサーブレット
パブリッククラスBaseServletは、httpservlet { / * *リクエストのメソッドに基づいてこのクラスのどのメソッドが呼び出されるかを決定します * /保護されたボイドサービス(httpservletrequest req、httpservletresponse res)servletexception、ioexception {req.setcharacterencoding( "utf-8"); res.setContentType( "text/html; charset = utf-8"); try {//例:http:// localhost:8080/demo1/xxx?m = add string method = req.getParameter( "method"); //それはメソッドですnameclass c = this.getclass();メソッドM = C.GetMethod(Method、httpservletrequest.class、httpservletresponse.class); string result =(string)m.invoke(this、req、res); if(result!= null &&!result.isempty()){req.getRequestDispatcher(result).forward(req、res); }} catch(Exception E){新しいServleTexception(e); }}}userservlet
/***ユーザー式レイヤー*/パブリッククラスユーザーサーブレット拡張baseservlet {private userservice userservice = new userservice(); / ** * exit function * @param request * @param Response * @return * @throws servletexception * @throws ioexception */ public string quit(httpservletrequest request、httpservletResponse応答)Servletexception、ioexception {request.getsession()。invalidate()。 "r:/index.jsp"を返します。 } public string login(httpservletrequest request、httpservletresponse応答)servletexception、ioexception { / * * 1。フォームデータをフォームにカプセル化 * 2にカプセル化 * 2。 commonutils.tobean(request.getParametermap()、user.class); try {user user = userservice.login(form); request.getSession()。setAttribute( "session_user"、user); /**ユーザーにショッピングカートを追加します。つまり、セッションにカートオブジェクトを保存*/ request.getSession()。setAttribute( "cart"、new cart()); "r:/index.jsp"を返します。 } catch(userexception e){request.setattribute( "msg"、e.getmessage()); request.setattribute( "form"、form); "f:/jsps/user/login.jsp"を返します。 }} /** * activation function * @param request * @param response * @return * @throws servletexception * @throws ioexception * /public string active(httpservletrequest request、httpservletresponse応答)スローServletexception、ioexception、ioexception { / * * depect expect excements complece exception { / * * msg.jsp * 3に転送します。成功情報をリクエストドメインに保存し、msg.jsp */ string code = request.getParameter( "code")に転送します。 try {userservice.active(code); request.setattribute( "msg"、 "おめでとうございます、あなたはそれを正常にアクティブにしました!今すぐログインしてください!"); } catch(userexception e){request.setattribute( "msg"、e.getmessage()); } "f:/jsps/msg.jsp"を返します。 } /** *登録機能 * @param request * @param Response * @return * @throws servletexception * @throws ioexception * /public string regist(httpservletrequest request、httpservletrequest Response)をスローSrevletexception、ioexception、ioexception { / * * 1。フィールド、Register.jsp * 4。サービスメソッドを呼び出して登録を完了します *>エラー情報を保存し、フォームにフィールドをリクエストし、登録することを要求します。jsp *5。msg.jsp */ // commonutils.tobean(request.getparametermap()、user.class); //完全なform.setuid(commonutils.uuid()); form.setcode(commonutils.uuid()) + commonutils.uuid()); /**検証を入力*1。エラー情報をカプセル化するマップを作成します。キーはフォームフィールド名で、値はエラーメッセージです*/ map <string、string> errors = new hashmap <string、string>(); /** 2。検証のためにユーザー名、パスワード、およびフォームの電子メールを取得します*/ string username = form.getUsername(); if(username == null || username.trim()。isempty()){errors.put( "username"、 "usernameは空にすることはできません!"); } else if(username.length()<3 || username.length()> 10){errors.put( "username"、 "usernameは3〜10!"); }文字列パスワード= form.getPassWord(); if(password == null || password.trim()。isempty()){errors.put( "password"、 "passwordは空にすることはできません!"); } else if(password.length()<3 || password.length()> 10){errors.put( "password"、 "passwordは3〜10!"でなければなりません); }文字列email = form.getemail(); if(email == null || email.trim()。isempty()){errors.put( "email"、 "emailは空にすることはできません!"); } else if(!email.matches( "// w+@//w+/.//w+")){errors.put( "email"、 "email Format error!"); }/** 3。エラーメッセージがあるかどうかを判断します*/ if(errors.size()> 0){// 1。エラーメッセージを保存// 2。フォームデータを保存// 3。 request.setattribute( "form"、form); "f:/jsps/user/regist.jsp"; } / * *サービスのregist()methodを呼び出します * / try {userservice.regist(form); } catch(userexception e){ / * * 1。例外情報を保存 * 2。フォームを保存 *3。regist.jsp * / request.setattribute( "msg"、e.getmessage()); request.setattribute( "form"、form); "f:/jsps/user/regist.jsp"; } /**電子メールを送信*構成ファイルを準備してください! */ //構成ファイルコンテンツプロパティを取得= new Properties(); propss.load(this.getClass()。getClassLoader().getResourceasStream( "email_template.properties")); string host = propss.getProperty( "host"); // get server host string uname = props.getProperty( "uname"); // get username string pwd = props.getProperty( "pwd"); propss.getProperty( "subject"); //件名文字列content = props.getProperty( "content"); //メールコンテンツコンテンツ= messageformat.format(content、form.getcode()); // form.getcode()) Mailutils.send(セッション、メール); //メールを送信してください! } catch(messagingexception e){} / * * 1。成功情報を保存 *2。msg.jsp * / request.setattribute( "msg"、 "おめでとうございます、登録が成功しました!すぐにメールアドレスでアクティブ化してください"); "f:/jsps/msg.jsp"を返します。 }}2.4ユーザーサービス
/***ユーザービジネスレイヤー*/public class userservice {private userdao userdao = new userdao(); / **登録関数* @Param Form*/ public void登録(ユーザーフォーム)スローuserexception {// verify username users user = userdao.findbyusername(form.getusername()); if(user!= null)新しいuserexception( "ユーザー名が登録されている!"); //メールユーザー= userdao.findbyemail(form.getemail()); if(user!= null)新しいuserexception( "emailが登録されている!"); //ユーザーをデータベースに挿入しますuserdao.add(form); } / ** * activation function * @throws userexception * / public void active(string code)throws userexception { / * * 1。コードを使用してデータベースをクエリしてユーザーユーザー= userdao.findbycode(code); /** 2。ユーザーが存在しない場合、アクティベーションコードが間違っていることを意味します*/ if(user == null)throw new userexception( "activationコードは無効です!"); / * * 3。ユーザーのステータスが非アクティブかどうかを確認します。アクティブ化されている場合、それは二次的なアクティベーションであり、例外がスローされることを意味します*/ if(user.isstate())新しいuserexceptionをスローします(「あなたはアクティブにされました、死にたいと思っていない限り再度アクティブにしないでください!」)。 /**4。ユーザーのステータスを変更*/ userdao.updatestate(user.getuid()、true); } /** * login function * @param form * @return * @throws userexception * /public user login(user form)throws userexception { / * * 1。ユーザー名を使用してユーザーを取得します * 2。異なる場合、例外がスローされます(パスワードが間違っています) * 4。ユーザーのステータスを確認します。 falseの場合、例外がスローされます(まだアクティブ化されていません) * 5。ユーザーを返します */ user user = userdao.findbyusername(form.getusername()); if(user == null)新しいuserexception( "ユーザー名は存在しません!"); if(!user.getPassWord()。 if(!user.isstate())は新しいuserexception( "まだアクティブ化されていない!")をスローします。ユーザーを返します。 }}2.5 userdao
/***ユーザーPersistence Layer*/public class userdao {private queryrunner qr = new txqueryrunner(); / ** * usernameによるクエリ * @param username * @return */ public user findbyusername(string username){try {string sql = "select * from tb_user where username =?"; return qr.query(sql、new beanhandler <user>(user.class)、username); } catch(sqlexception e){新しいruntimeexception(e); }} / ** * email * @param email * @return * / public user findbyemail(string email){try {string sql = "select * from tb_user where email =?"; return qr.query(sql、new beanhandler <user>(user.class)、email); } catch(sqlexception e){新しいruntimeexception(e); }} / ** * insert user * @param user * / public void add(user user){try {string sql = "tb_user値に挿入(?、?、?、?、?、?)"; object = {user.getuid()、user.getusername()、user.getPassWord()、user.getEmail()、user.getCode()、user.isstate()}; qr.update(sql、params); } catch(sqlexception e){新しいruntimeexception(e); }} / ** *アクティベーションコードによるクエリ * @param code * @return * / public user findbycode(string code){try {string sql = "select * from tb_user where code =?"; return qr.query(sql、new beanhandler <user>(user.class)、code); } catch(sqlexception e){新しいruntimeexception(e); }} / ** *指定されたユーザーの指定された状態を変更します * @param uid * @param state * / public void updatestate(string uid、boolean state){try {string sql = "更新tb_user set state =?where uid =?"; Qr.Update(SQL、STATE、UID); } catch(sqlexception e){新しいruntimeexception(e); }}}3。ユーザーのアクティベーション
プロセス:ユーザーの電子メール - > userservlet#active() - > msg.jsp
4。
ユーザーログイン
Process:/jsps/user/login.jsp-> userservlet#login() - > index.jsp
5。
ユーザー終了
プロセス:top.jsp-> userservlet#quit() - > login.jsp
quit():セッションを破壊します!
上記はこの記事のすべての内容です。私はそれがすべての人の学習に役立つことを願っています、そして、私は誰もがwulin.comをもっとサポートすることを願っています。