本文主要整理了一下老師講的註冊流程:java
首先註冊頁面regist.jsp將表單數據提交到UerServlet的regist方法中:
web
1.封裝表單數據到User form對象中。
sql
2.補全:uid,code(激活碼) ,經過commonUtils工具包的uuid()生成隨機碼
數據庫
3.輸入效驗(不訪問數據庫的),保存錯誤信息到request,保存form到request中用於回顯,轉發回到regist.jsp頁面
apache
4.調用service的regist(form),保存異常信息到request,保存form到request中用於回顯,轉發到regist.jsp服務器
5.發送郵件
session
6.保存成功信息到request
dom
7.轉發到msg.jspjsp
其次userService中的regist方法:
工具
1.效驗formde username是否已被註冊。拋出異經常使用戶已被註冊
2.效驗form的email是否已被註冊。拋出異常Email已被註冊
3.把form保存到數據庫中
最後userDao:
1.User findByUsername(String username)
2.User findByEmail(String email)
3.void add(User user)
激活時,將所須要的參數寫在配置文件中,須要知道發送郵件的主機也就是郵件服務器。如:host=smtp.163.com,其次是發送方的用戶名和密碼,來自哪一個地方的郵件,以及主題和內容,如:uname=****,pwd=**** from=***@163.com
content=<a href="http://localhost:8080/bookstore/UserServlet?method=active&code={0}">
UserServlet的active方法:
1.獲取參數:激活碼
2.使用激活碼調用service中的active(String code)方法,保存異常信息到request中,轉發到msg.jsp
3.保存激活成功信息到request中
4.轉發到msg.jsp中
UserService的active(String code):
1.使用code去查詢數據庫,獲得User對象,若是數據庫返回的是null,拋出異常
2.查看用戶的狀態。true拋處異常表示已經成功的註冊了無需重複註冊。false修改用戶的狀態爲true
UserDao:
1.User findByCode(String code)
2.void updateState(String uid,boolean state)
UserDao層代碼:
package cn.itcast.bookstore.user.dao; import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import cn.itcast.bookstore.user.domain.User; import cn.itcast.jdbc.TxQueryRunner; /** * User持久層 * @author cxf * */ public class UserDao { private QueryRunner qr = new TxQueryRunner(); /** * 按用戶名查詢 * @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) { throw new RuntimeException(e); } } /** * 按用戶名查詢 * @param username * @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) { throw new RuntimeException(e); } } /** * 插入User * @param user */ public void add(User user) { try { String sql = "insert into tb_user values(?,?,?,?,?,?)"; Object[] params = {user.getUid(), user.getUsername(), user.getPassword(), user.getEmail(), user.getCode(), user.isState()}; qr.update(sql, params); } catch(SQLException e) { throw new 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) { throw new RuntimeException(e); } } /** * 修改指定用戶的指定狀態 * @param uid * @param state */ public void updateState(String uid, boolean state) { try { String sql = "update tb_user set state=? where uid=?"; qr.update(sql, state, uid); } catch(SQLException e) { throw new RuntimeException(e); } } }
UserService的代碼:
package cn.itcast.bookstore.user.service; import cn.itcast.bookstore.user.dao.UserDao; import cn.itcast.bookstore.user.domain.User; /** * User業務層 * @author cxf * */ public class UserService { private UserDao userDao = new UserDao(); /** * 註冊功能 * @param form */ public void regist(User form) throws UserException{ // 校驗用戶名 User user = userDao.findByUsername(form.getUsername()); if(user != null) throw new UserException("用戶名已被註冊!"); // 校驗email user = userDao.findByEmail(form.getEmail()); if(user != null) throw new UserException("Email已被註冊!"); // 插入用戶到數據庫 userDao.add(form); } /** * 激活功能 * @throws UserException */ public void active(String code) throws UserException { /* * 1. 使用code查詢數據庫,獲得user */ User user = userDao.findByCode(code); /* * 2. 若是user不存在,說明激活碼錯誤 */ if(user == null) throw new UserException("激活碼無效!"); /* * 3. 校驗用戶的狀態是否爲未激活狀態,若是已激活,說明是二次激活,拋出異常 */ if(user.isState()) throw new UserException("您已經激活過了,不要再激活了,除非你想死!"); /* * 4. 修改用戶的狀態 */ userDao.updateState(user.getUid(), true); } /** * 登陸功能 * @param form * @return * @throws UserException */ public User login(User form) throws UserException { /* * 1. 使用username查詢,獲得User * 2. 若是user爲null,拋出異常(用戶名不存在) * 3. 比較form和user的密碼,若不一樣,拋出異常(密碼錯誤) * 4. 查看用戶的狀態,若爲false,拋出異常(還沒有激活) * 5. 返回user */ User user = userDao.findByUsername(form.getUsername()); if(user == null) throw new UserException("用戶名不存在!"); if(!user.getPassword().equals(form.getPassword())) throw new UserException("密碼錯誤!"); if(!user.isState()) throw new UserException("還沒有激活!"); return user; } }
UserServlet的相關代碼:
package cn.itcast.bookstore.user.web.servlet; import java.io.IOException; import java.text.MessageFormat; import java.util.HashMap; import java.util.Map; import java.util.Properties; import javax.mail.MessagingException; import javax.mail.Session; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.itcast.bookstore.user.domain.User; import cn.itcast.bookstore.user.service.UserException; import cn.itcast.bookstore.user.service.UserService; import cn.itcast.commons.CommonUtils; import cn.itcast.mail.Mail; import cn.itcast.mail.MailUtils; import cn.itcast.servlet.BaseServlet; /** * User表述層 */ public class UserServlet extends BaseServlet { private UserService userService = new UserService(); /** * 退出功能 * @param request * @param response * @return * @throws ServletException * @throws IOException */ public String quit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getSession().invalidate(); return "r:/index.jsp"; } public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1. 封裝表單數據到form中 * 2. 輸入校驗(不寫了) * 3. 調用service完成激活 * > 保存錯誤信息、form到request,轉發到login.jsp * 4. 保存用戶信息到session中,而後重定向到index.jsp */ User form = CommonUtils.toBean(request.getParameterMap(), User.class); try { User user = userService.login(form); request.getSession().setAttribute("session_user", user); return "r:/index.jsp"; } catch (UserException e) { request.setAttribute("msg", e.getMessage()); request.setAttribute("form", form); return "f:/jsps/user/login.jsp"; } } /** * 激活功能 * @param request * @param response * @return * @throws ServletException * @throws IOException */ public String active(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1. 獲取參數激活碼 * 2. 調用service方法完成激活 * > 保存異常信息到request域,轉發到msg.jsp * 3. 保存成功信息到request域,轉發到msg.jsp */ String code = request.getParameter("code"); try { userService.active(code); request.setAttribute("msg", "恭喜,您激活成功了!請立刻登陸!"); } catch (UserException e) { request.setAttribute("msg", e.getMessage()); } return "f:/jsps/msg.jsp"; } /** * 註冊功能 * @param request * @param response * @return * @throws ServletException * @throws IOException */ public String regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1. 封裝表單數據到form對象中 * 2. 補全:uid、code * 3. 輸入校驗 * > 保存錯誤信息、form到request域,轉發到regist.jsp * 4. 調用service方法完成註冊 * > 保存錯誤信息、form到request域,轉發到regist.jsp * 5. 發郵件 * 6. 保存成功信息轉發到msg.jsp */ // 封裝表單數據 User form = CommonUtils.toBean(request.getParameterMap(), User.class); // 補全 form.setUid(CommonUtils.uuid()); form.setCode(CommonUtils.uuid() + CommonUtils.uuid()); /* * 輸入校驗 * 1. 建立一個Map,用來封裝錯誤信息,其中key爲表單字段名稱,值爲錯誤信息 */ Map<String,String> errors = new HashMap<String,String>(); /* * 2. 獲取form中的username、password、email進行校驗 */ String username = form.getUsername(); if(username == null || username.trim().isEmpty()) { errors.put("username", "用戶名不能爲空!"); } else if(username.length() < 3 || username.length() > 10) { errors.put("username", "用戶名長度必須在3~10之間!"); } String password = form.getPassword(); if(password == null || password.trim().isEmpty()) { errors.put("password", "密碼不能爲空!"); } else if(password.length() < 3 || password.length() > 10) { errors.put("password", "密碼長度必須在3~10之間!"); } String email = form.getEmail(); if(email == null || email.trim().isEmpty()) { errors.put("email", "Email不能爲空!"); } else if(!email.matches("\\w+@\\w+\\.\\w+")) { errors.put("email", "Email格式錯誤!"); } /* * 3. 判斷是否存在錯誤信息 */ if(errors.size() > 0) { // 1. 保存錯誤信息 // 2. 保存表單數據 // 3. 轉發到regist.jsp request.setAttribute("errors", errors); request.setAttribute("form", form); return "f:/jsps/user/regist.jsp"; } /* * 調用service的regist()方法 */ try { userService.regist(form); } catch (UserException e) { /* * 1. 保存異常信息 * 2. 保存form * 3. 轉發到regist.jsp */ request.setAttribute("msg", e.getMessage()); request.setAttribute("form", form); return "f:/jsps/user/regist.jsp"; } /* * 發郵件 * 準備配置文件! */ // 獲取配置文件內容 Properties props = new Properties(); props.load(this.getClass().getClassLoader() .getResourceAsStream("email_template.properties")); String host = props.getProperty("host");//獲取服務器主機 String uname = props.getProperty("uname");//獲取用戶名 String pwd = props.getProperty("pwd");//獲取密碼 String from = props.getProperty("from");//獲取發件人 String to = form.getEmail();//獲取收件人 String subject = props.getProperty("subject");//獲取主題 String content = props.getProperty("content");//獲取郵件內容 content = MessageFormat.format(content, form.getCode());//替換{0} Session session = MailUtils.createSession(host, uname, pwd);//獲得session Mail mail = new Mail(from, to, subject, content);//建立郵件對象 try { MailUtils.send(session, mail);//發郵件! } catch (MessagingException e) { } /* * 1. 保存成功信息 * 2. 轉發到msg.jsp */ request.setAttribute("msg", "恭喜,註冊成功!請立刻到郵箱激活"); return "f:/jsps/msg.jsp"; } }