|
| 1 | +/* |
| 2 | + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license |
| 3 | + * Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template |
| 4 | + */ |
| 5 | +package com.library.controller.user; |
| 6 | + |
| 7 | +import com.library.factory.ServiceFactory; |
| 8 | +import com.library.service.UserService; |
| 9 | +import com.library.util.HashPassword; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.PrintWriter; |
| 12 | +import jakarta.servlet.ServletException; |
| 13 | +import jakarta.servlet.annotation.WebServlet; |
| 14 | +import jakarta.servlet.http.HttpServlet; |
| 15 | +import jakarta.servlet.http.HttpServletRequest; |
| 16 | +import jakarta.servlet.http.HttpServletResponse; |
| 17 | +import jakarta.servlet.http.HttpSession; |
| 18 | + |
| 19 | +/** |
| 20 | + * |
| 21 | + * @author laptop gigabyte |
| 22 | + */ |
| 23 | +@WebServlet(name = "ChangePassword", urlPatterns = {"/user/change-password"}) |
| 24 | +public class ChangePassword extends HttpServlet { |
| 25 | + |
| 26 | + private UserService userService = ServiceFactory.getUserService(); |
| 27 | + |
| 28 | + @Override |
| 29 | + protected void doPost(HttpServletRequest request, HttpServletResponse response) |
| 30 | + throws ServletException, IOException { |
| 31 | + String currentPassword = request.getParameter("currentPassword"); |
| 32 | + String newPassword = request.getParameter("newPassword"); |
| 33 | + String confirmPassword = request.getParameter("confirmPassword"); |
| 34 | + HttpSession session = request.getSession(false); |
| 35 | + String account = (String) session.getAttribute("account"); |
| 36 | + String getHashedPassword = userService.getHashedPassword(account); |
| 37 | + String hashedPassword = HashPassword.hash(newPassword); |
| 38 | + if (newPassword.trim().isEmpty()) { |
| 39 | + session.setAttribute("changePasswordError", "Mật khẩu không được bao gồm khoảng trắng!"); |
| 40 | + response.sendRedirect(request.getContextPath() + "/user/setting"); |
| 41 | + return; |
| 42 | + } else if (newPassword.contains(" ")) { |
| 43 | + session.setAttribute("changePasswordError", "Mật khẩu không được bao gồm khoảng trắng!"); |
| 44 | + response.sendRedirect(request.getContextPath() + "/user/setting"); |
| 45 | + return; |
| 46 | + }else if(!newPassword.equals(confirmPassword)){ |
| 47 | + session.setAttribute("changePasswordError", "Mật khẩu xác nhận không giống mật khẩu mới!"); |
| 48 | + response.sendRedirect(request.getContextPath() + "/user/setting"); |
| 49 | + return; |
| 50 | + } |
| 51 | + if (HashPassword.checkPassword(currentPassword, getHashedPassword)) { |
| 52 | + if (userService.updatePassword(account, hashedPassword)) { |
| 53 | + session.setAttribute("changePasswordSuccess", "Cập nhật mật khẩu thành công!"); |
| 54 | + response.sendRedirect(request.getContextPath() + "/user/setting"); |
| 55 | + return; |
| 56 | + } else { |
| 57 | + session.setAttribute("changePasswordError", "Cập nhật mật khẩu không thành công!"); |
| 58 | + response.sendRedirect(request.getContextPath() + "/user/setting"); |
| 59 | + return; |
| 60 | + } |
| 61 | + } else { |
| 62 | + session.setAttribute("changePasswordError", "Mật khẩu hiện tại sai!"); |
| 63 | + response.sendRedirect(request.getContextPath() + "/user/setting"); |
| 64 | + return; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments