本文主要整理了一下老师讲的注册流程:
首先注册页面regist.jsp将表单数据提交到UerServlet的regist方法中:
1.封装表单数据到User form对象中。
2.补全:uid,code(激活码) ,通过commonUtils工具包的uuid()生成随机码
3.输入效验(不访问数据库的),保存错误信息到request,保存form到request中用于回显,转发回到regist.jsp页面
4.调用service的regist(form),保存异常信息到request,保存form到request中用于回显,转发到regist.jsp
5.发送邮件
6.保存成功信息到request
7.转发到msg.jsp
其次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";
}
}