|
|
|
@ -29,83 +29,85 @@ import cn.hutool.crypto.SecureUtil;
|
|
|
|
|
public class MemberLoginServlet extends HttpServlet {
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理POST请求的方法。
|
|
|
|
|
* 当客户端发送POST请求到此Servlet时,这个方法会被调用。
|
|
|
|
|
* @param request HttpServletRequest对象,包含客户端请求信息。
|
|
|
|
|
* @param response HttpServletResponse对象,用于发送响应到客户端。
|
|
|
|
|
* @throws ServletException 可能抛出的Servlet异常。
|
|
|
|
|
* @throws IOException 可能抛出的IO异常。
|
|
|
|
|
*/
|
|
|
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
|
throws ServletException, IOException {
|
|
|
|
|
// 从请求中获取用户名和密码参数
|
|
|
|
|
String userName = request.getParameter("userName");
|
|
|
|
|
String passWord = request.getParameter("passWord");
|
|
|
|
|
|
|
|
|
|
// md5加密
|
|
|
|
|
//String passWord = SecureUtil.md5(passwd);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建AdminService对象,用于查询管理员信息
|
|
|
|
|
AdminService adminService = new AdminServiceImpl();
|
|
|
|
|
// 根据用户名查询管理员
|
|
|
|
|
Admin admin = adminService.getAdminByName(userName);
|
|
|
|
|
|
|
|
|
|
// 创建Session,存储登录的用户对象
|
|
|
|
|
// 创建Session对象,用于存储登录的用户信息
|
|
|
|
|
HttpSession session = request.getSession();
|
|
|
|
|
// 设置Session的超时时间为30分钟(30*60秒)
|
|
|
|
|
session.setMaxInactiveInterval(30 * 60);
|
|
|
|
|
|
|
|
|
|
// 获取PrintWriter对象,用于向客户端发送响应
|
|
|
|
|
PrintWriter out = response.getWriter();
|
|
|
|
|
|
|
|
|
|
// 判断是否是管理员账号
|
|
|
|
|
// 判断查询到的用户是否为管理员
|
|
|
|
|
if (admin != null) {
|
|
|
|
|
// 如果密码匹配
|
|
|
|
|
if (admin.getPassword().equals(passWord)) {
|
|
|
|
|
|
|
|
|
|
// 更新登录状态和登录时间
|
|
|
|
|
// 更新管理员的登录状态和登录时间
|
|
|
|
|
admin.setIsUse(1);
|
|
|
|
|
admin.setLoginTime(DateUtil.now());
|
|
|
|
|
adminService.updateAdmin(admin);
|
|
|
|
|
|
|
|
|
|
// 将登录对象存入session
|
|
|
|
|
// 将管理员对象存入Session
|
|
|
|
|
session.setAttribute("admin", admin);
|
|
|
|
|
|
|
|
|
|
// 跳转到管理界面
|
|
|
|
|
// 使用JavaScript弹出登录成功提示,并跳转到管理界面
|
|
|
|
|
out.write("<script>alert('登录成功!欢迎你," + admin.getUserName() + "');"
|
|
|
|
|
+ "window.location.href='pages/admin/adminMain.jsp'</script>");
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
out.write(
|
|
|
|
|
"<script>alert('密码错误,请重新输入!');" + "window.location.href='pages/user/memberLogin.jsp'</script>");
|
|
|
|
|
|
|
|
|
|
// 如果密码不匹配,弹出密码错误的提示,并跳转到登录页面
|
|
|
|
|
out.write("<script>alert('密码错误,请重新输入!');"
|
|
|
|
|
+ "window.location.href='pages/user/memberLogin.jsp'</script>");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// 判断是否是会员账号
|
|
|
|
|
// 如果查询到的用户不是管理员,再查询是否为会员
|
|
|
|
|
MemberService memberService = new MemberServiceImpl();
|
|
|
|
|
Member member = memberService.getMemberByName(userName);
|
|
|
|
|
if (member != null) {
|
|
|
|
|
// 密码正确,转到首页
|
|
|
|
|
// 如果密码匹配
|
|
|
|
|
if (passWord.equals(member.getPassword())) {
|
|
|
|
|
|
|
|
|
|
// 先修改数据库登录状态和登录时间
|
|
|
|
|
// 更新会员的登录状态和登录时间
|
|
|
|
|
member.setIfUse(1);
|
|
|
|
|
member.setLogintimes(DateUtil.now());
|
|
|
|
|
memberService.updateMember(member);
|
|
|
|
|
|
|
|
|
|
// 将登录信息存入session
|
|
|
|
|
/*
|
|
|
|
|
* HttpSession session = request.getSession();
|
|
|
|
|
* session.setMaxInactiveInterval(30 * 60);
|
|
|
|
|
*/
|
|
|
|
|
// 将会员对象存入Session
|
|
|
|
|
session.setAttribute("member", member);
|
|
|
|
|
|
|
|
|
|
// 跳转到首页
|
|
|
|
|
// 使用JavaScript弹出登录成功提示,并跳转到首页
|
|
|
|
|
out.write("<script>alert('登录成功!欢迎你," + member.getUserName() + "');"
|
|
|
|
|
+ "window.location.href='pages/user/home.jsp'</script>");
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// 如果密码不匹配,弹出密码错误的提示,并跳转到登录页面
|
|
|
|
|
out.write("<script>alert('密码错误,请重新输入!');"
|
|
|
|
|
+ "window.location.href='pages/user/memberLogin.jsp'</script>");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
out.write("<script>alert('该用户不存在!');" + "window.location.href='pages/user/memberLogin.jsp'</script>");
|
|
|
|
|
// 如果用户既不是管理员也不是会员,弹出用户不存在的提示,并跳转到登录页面
|
|
|
|
|
out.write("<script>alert('该用户不存在!');"
|
|
|
|
|
+ "window.location.href='pages/user/memberLogin.jsp'</script>");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 关闭PrintWriter对象
|
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|