RegServlet类 Post方法 添加如下代码:
if(isRegSuccess){
//发送激活邮件
String emailMsg = "恭喜您注册成功,请点击下面的连接进行激活账户"
+ "<a href='http://localhost:8080/xiangmuming/active?activeCode="
+activeCode+"'>"+"http://localhost:8080/xiangmuming/active?activeCode="+activeCode+"</a>";
try {
MailUtils.sendMail(user.getEmail(), emailMsg);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.sendRedirect(request.getContextPath()+"/registerSuccess.jsp");
}else{
response.sendRedirect(request.getContextPath()+"/registerFail.jsp");
}
UserDao类
// 激活
public void active(String code) {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "update user set state=? where code=?";
try {
runner.update(sql, 1, code);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
UserService类
//激活用户
public void active(String code){
userDao.active(code);
}
4.ActiveServlet类
public class ActiveServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserService userService=new UserService();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String activeCode=request.getParameter("activeCode");
userService.active(activeCode);
response.sendRedirect(request.getContextPath()+"/login.jsp");
}
}
以上代码在本人项目中已运行成功,如有不恰当地方请各位大神多多指教