1.用户激活的思想
- UserServlet#activation()方法内容如下:
获取激活码;
调用UserService#findByActivationCode(String)得到User对象;
如果UserService#findByActivationCode(String)方法抛出了UserException,那么保存异常信息,转发到msg.jsp显示错误信息,return;
如果没有抛出异常,说明激活成功,保存成功信息,转发到msg.jsp;
- UserService#activation(String activationCode)方法内容如下:
使用UserDao#findByActivationCode(String)查询User;
如果User为null,抛出UserException;
否则,查看User的status是否为true,如果为true说明已经当前用户已经激活过了,所以抛出UserException;
调用UserDao#updateStatus(Stringuid, boolean status)方法,修改当前当前用户状态为true;
- UserDao需要提供两个方法:
UserfindByActivationCode(String activationCode):通过激活码查询用户;
voidupdateStatus(String uid, boolean status):通过uid修改状态。
2.dao模块的编写
编写两个函数,一个通过激活码查询用户,一个通过uid修改用户
UserDao.java
/**
* 通过激活码查找用户,并返回User
* @param activationCode
* @return
* @throws SQLException
*/
public User findByActivationCode(String activationCode) throws SQLException{
String sql="select *from t_user where activationCode=?";
User user=(User) query.query(sql, new BeanHandler<User>(User.class), activationCode);
return user;
}
/**
* 通过用户的UId,来更新用户的激活状态
* @param uid
* @param status
* @throws SQLException
*/
public void updateStatus(String uid,boolean status) throws SQLException{
String sql="update t_user set status=1 where uid=?";
query.update(sql, uid);
}
3.UserException.java的编写
继承Exception类,重写父类的方法
package com.tao.user.service.exception;
/**
* 自定义用户异常,以便于捕获异常
* @author tao
*
*/
public class UserException extends Exception{
public UserException() {
super();
// TODO Auto-generated constructor stub
}
public UserException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public UserException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public UserException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
4.UserService.java的编写
添加一个activation()方法
调用dao层的findByActivationCode()方法
返回User,如果User为空,跑UserException异常
如果激活码已激活,抛UserException异常
然后调用dao层的updateStatus()方法
/**
* 先把激活码传过去,然后调用dao层的findByActivationCode()方法,得到当前User
* 如果user为空,则抛UserException异常
* 如果用户已经激活,也抛UserException异常
* @param activationCode
* @throws UserException
*/
public void activation(String activationCode) throws UserException{
try {
User user=userDao.findByActivationCode(activationCode);
if(user==null) throw new UserException("激活码错误");
if(user.isStatus()) throw new UserException("用户以激活!");
userDao.updateStatus(user.getUid(), true);
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
5.UserSevlet.java的编写
添加一个activation()方法
调用servie层的activatio()方法
捕获异常,如果有异常,则将异常信息发送到msg,jsp页面
否则,将成功信息发送到msg,jsp页面
/**
* 首先得到激活码
* 再调用service层的activation()方法,激活
* 如果service层抛出异常,则说明激活失败
* 则我们捕获异常,将激活成功与失败信息传到msg.jsp页面中
* @param request
* @param response
* @return
* @throws ServletException
* @throws IOException
* @throws SQLException
*/
public String activation(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
String activationCode=request.getParameter("activationCode");
// System.out.println(activationCode);
try {
userService.activation(activationCode);
request.setAttribute("msg", "你已激活成功,请登录!");
request.setAttribute("code", "success");
} catch (UserException e) {
// TODO Auto-generated catch block
request.setAttribute("msg", e.getMessage());
request.setAttribute("code", "error");
}
return "f:jsps/msg.jsp";
}