08 "enable user mailbox activation

Posted by sarika on Mon, 02 Dec 2019 15:00:51 +0100

Mailbox activation implementation

  • process analysis
  • code implementation

1) process analysis

① click the link to activate the email: http://localhost:8080/store/user/active?code=xxx

② it is necessary to write a process for / active path in UserServlet

  • Get activation code
  • Call service layer to complete activation
  • Page Jump, request to forward jsp/msg.jsp

③ create active (String code) in UserService

  • Get a user by activation code (the user may be empty)
  • If it is not empty, modify the user's state and change 0 to 1

2) code implementation

① implement path / user/active processing in UserServlet

There are three main steps in servlet processing
1. Get activation code
2. Call service to complete activation
3. Forward the request to msg.jsp

/**
 * User activation
 * @param request
 * @param response
 * @throws IOException 
 * @throws ServletException 
 */
private void active(HttpServletRequest request, HttpServletResponse response) throws Exception{
	//1. Get activation code
	String code=request.getParameter("code");
	
	//2. Call service to complete activation
	User user=userService.active(code);
	if(user==null) {
		//No user found by activation code
		request.setAttribute("msg", "Please reactivate");
	}else {
		//Add information
		request.setAttribute("msg", "Successful activation");
	}
	//3. Forward the page request to msg.jsp
	request.getRequestDispatcher("/jsp/msg.jsp").forward(request, response);
}
② write user activation method active in UserService

1. Get a user by activation code
2. Judge whether the user is empty
3. Modify user status 0 - > 1
4. Call dao to perform modification
UserService interface:

/**
 * User activation
 * @param code
 * @return
 */
User active(String code) throws Exception;

UserServiceImpl implementation class:

@Override
public User active(String code) throws Exception{
	//1. Get a user by code
	User user=userDao.getUserByCode(code);
	//2. Judge whether the user is null
	if(user==null) {
		//2.1 user is null -- return null directly
		return null;
	}
	//3. Modify user status 0 - > 1
	user.setState(1);
	userDao.update(user);
	return user;
}
③ implement UserDao

1. Get users according to code activation code
2. Modify user
UserDao interface:

/**
 * Get users according to code activation code
 * @param code
 * @return
 */
User getUserByCode(String code)  throws SQLException ;
/**
 * User modification
 * @param user
 */
void update(User user) throws SQLException ;

UserDaoImpl:

/**
 * Get a user by activation code
 */
@Override
public User getUserByCode(String code) throws SQLException {
	String sql="select * from `user` where code=? limit 1";
	return qr.query(sql, new BeanHandler<User>(User.class),code);
}
/**
 * Modify user information
 */
@Override
public void update(User user) throws SQLException {
	String sql="update `user` set username=?,password=?,name=?,email=?,birthday=?,state=?,code=? where uid=?";
	qr.update(sql,user.getUsername(),user.getPassword(),user.getName(),user.getEmail(),user.getBirthday(),user.getState(),null,user.getUid());
}
④ test mailbox activation

First, add some characters after the activation code, and test a nonexistent activation code:

Then activate the test with the correct activation code:

Topics: JSP SQL