传智书城前台用户登录实现

Web应用开发 专栏收录该内容
1 篇文章 0 订阅

1.激活码
在这里插入图片描述
代码:

package com.xhq.web.servlet.client;

import com.xhq.exception.ActiveUserException;
import com.xhq.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@SuppressWarnings("serial")
public class ActiveUserServlet extends HttpServlet {
   @Override
   public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      doPost(request, response);
   }

   @Override
   public void doPost(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      // 1.获取激活码
      String activeCode = request.getParameter("activeCode");
      // 2.调用service中激活用户操作
      UserService service = new UserService();
      try {
         service.activeUser(activeCode);
         response.sendRedirect(request.getContextPath() + "/client/activesuccess.jsp");
         return;
      } catch (ActiveUserException e) {
         e.printStackTrace();
         response.getWriter().write(e.getMessage());
         return;
      }
   }
}
package com.xhq.service;

import com.xhq.dao.UserDao;
import com.xhq.domain.User;
import com.xhq.exception.ActiveUserException;
import com.xhq.exception.RegisterException;
import com.xhq.utils.MailUtils;

import javax.security.auth.login.LoginException;
import java.sql.SQLException;
import java.util.Date;

public class UserService {
   private UserDao dao = new UserDao();
   // 注册操作
   public void register(User user) throws RegisterException {
      // 调用dao完成注册操作
      try {
         dao.addUser(user);
         // 发送激活邮件
         String emailMsg = "感谢您注册网上书城,点击"
               + "<a href='http://localhost:8080/bookstore/activeUser?activeCode="
               + user.getActiveCode() + "'>&nbsp;激活&nbsp;</a>后使用。"
                     + "<br />为保障您的账户安全,请在24小时内完成激活操作";
         MailUtils.sendMail(user.getEmail(), emailMsg);
      } catch (Exception e) {
         e.printStackTrace();
         throw new RegisterException("注冊失败");
      }
   }
   // 激活用户
   public void activeUser(String activeCode) throws ActiveUserException {
      try {
         // 根据激活码查找用户
         User user = dao.findUserByActiveCode(activeCode);
         if (user == null) {
            throw new ActiveUserException("激活用户失败");
         }
         // 判断激活码是否过期 24小时内激活有效.
         // 1.得到注册时间
         Date registTime = user.getRegistTime();
         // 2.判断是否超时
         long time = System.currentTimeMillis() - registTime.getTime();
         if (time / 1000 / 60 / 60 > 24) {
            throw new ActiveUserException("激活码过期");
         }
         // 激活用户,就是修改用户的state状态
         dao.activeUser(activeCode);
      } catch (SQLException e) {
         e.printStackTrace();
         throw new ActiveUserException("激活用户失败");
      }
   }
   // 登录操作
   public User login(String username, String password) throws LoginException {
      try {
         //根据登录时表单输入的用户名和密码,查找用户
         User user = dao.findUserByUsernameAndPassword(username, password);
         //如果找到,还需要确定用户是否为激活用户
         if (user != null) {
            // 只有是激活才能登录成功,否则提示“用户未激活”
            if (user.getState() == 1) {
               return user;
            }
            throw new LoginException("用户未激活");
         }
         throw new LoginException("用户名或密码错误");
      } catch (SQLException e) {
         e.printStackTrace();
         throw new LoginException("登录失败");
      }
   }
   // 修改密码
   public boolean updatePwd(String username, String password,String telephone) throws Exception {
      boolean flag = false;
      try {
         if(dao.updatePwd(username, password,telephone)>0){
            flag = true;
         }
      }catch (Exception e){
         e.printStackTrace();
      }
      return flag;
   }
}
package com.xhq.dao;

import com.xhq.domain.User;
import com.xhq.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDao {
   // 添加用户
   public void addUser(User user) throws SQLException {
      String sql = "insert into user(username,password,gender,email,telephone,introduce,activecode) values(?,?,?,?,?,?,?)";
      QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
      int row = runner.update(sql, user.getUsername(), user.getPassword(),
            user.getGender(), user.getEmail(), user.getTelephone(),
            user.getIntroduce(), user.getActiveCode());
      if (row == 0) {
         throw new RuntimeException();
      }
   }

   // 根据激活码查找用户
   public User findUserByActiveCode(String activeCode) throws SQLException {
      String sql = "select * from user where activecode=?";
      QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
      return runner.query(sql, new BeanHandler<User>(User.class), activeCode);

   }

   // 激活用戶
   public void activeUser(String activeCode) throws SQLException {
      String sql = "update user set state=? where activecode=?";
      QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
      runner.update(sql, 1, activeCode);
   }
   
   //根据用户名与密码查找用户
   public User findUserByUsernameAndPassword(String username, String password) throws SQLException {
      String sql="select * from user where username=? and password=?";
      QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
      return runner.query(sql, new BeanHandler<User>(User.class),username,password);
   }

   //  修改密码
   public int updatePwd(String username, String password,String telephone) throws SQLException {
      String sql="update user set PASSWORD = ? , telephone = ? where username = ?";
      QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
      return runner.update(sql,password,telephone,username);
   }
}
package com.xhq.exception;
//激活用户
public class ActiveUserException extends Exception {

   private static final long serialVersionUID = 1L;

   public ActiveUserException() {
      super();
   }

   public ActiveUserException(String message, Throwable cause) {
      super(message, cause);
   }

   public ActiveUserException(String message) {
      super(message);
   }

   public ActiveUserException(Throwable cause) {
      super(cause);
   }

}

2.登录过程执行
在这里插入图片描述
代码:

package com.xhq.web.servlet.client;

import com.xhq.domain.User;
import com.xhq.service.UserService;

import javax.security.auth.login.LoginException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
   private static final long serialVersionUID = 1L;
   @Override
   public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      doPost(request, response);
   }
   @Override
   public void doPost(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      // 1.获取登录页面输入的用户名与密码
      String username = request.getParameter("username");
      String password = request.getParameter("password");
      // 2.调用service完成登录操作。
      UserService service = new UserService();
      try {
         User user = service.login(username, password);
         
         // 3.登录成功,将用户存储到session中.
         request.getSession().setAttribute("user", user);
         // 获取用户的角色,其中用户的角色分普通用户和超级用户两种
         String role = user.getRole();
         // 如果是超级用户,就进入到网上书城的后台管理系统;否则进入我的账户页面
         if ("超级用户".equals(role)) {
            response.sendRedirect(request.getContextPath() + "/admin/login/home.jsp");
            return;
         } else {
            response.sendRedirect(request.getContextPath() + "/client/myAccount.jsp");
            return;
         }
      } catch (LoginException e) {
         // 如果出现问题,将错误信息存储到request范围,并跳转回登录页面显示错误信息
         e.printStackTrace();
         request.setAttribute("register_message", e.getMessage());
         request.getRequestDispatcher("/client/login.jsp").forward(request, response);
         return;
      }
   }
}
  • 3
    点赞
  • 0
    评论
  • 10
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值