我将确认代码链接发送到注册用户的电子邮件中,当单击链接时,我需要激活状态以批准会员资格,但我收到标题中显示的错误。一切都是真的,但我不明白为什么会这样。这是我的代码:
1)在此代码部分,用户成为会员,确认代码将发送到他的电子邮件。
@WebServlet(name = "RegisterUserSaveController", urlPatterns = {"/register-user-save"})
public class RegisterUserSaveController extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String name = request.getParameter("name");
String surname = request.getParameter("surname");
String email = request.getParameter("email");
String password = request.getParameter("password");
String repeatPassword = request.getParameter("password-repeat");
if (!password.equals(repeatPassword)) {
request.setAttribute("error", "Password repeat is not same!");
request.getRequestDispatcher("register").forward(request, response);
} else {
User user = new User();
user.setName(name);
user.setSurname(surname);
user.setEmail(email);
user.setPassword(PasswordHasher.hashPassword(password));
String activationCode = UUID.randomUUID().toString();
user.setActivationCode(MD5.hashedMd5(activationCode));
LocalDateTime expiredDate = LocalDateTime.now().plusHours(1);
user.setExpiredDate(expiredDate);
UserDaoService userDaoService = new UserDaoManager();
userDaoService.save(user);
String subject = "Confirm Registration";
String link = "http://localhost:8084/employee/registerconfirm?code=" + activationCode;
String title = "Your confirmation link:\n " + link;
SendEmail.sendAsync(email, title, subject);
request.setAttribute("info2", "Your Registration was successfully! Pls check your email.");
request.getRequestDispatcher("success-info").forward(request, response);
}
} catch (GeneralSecurityException e) {
}
2)在此代码部分,执行用户点击发送到电子邮件的链接后的步骤。因此,如果代码正确,我将验证数据库中用户的状态列。
@WebServlet(name = "RegisterConfirmController", urlPatterns = {"/registerconfirm"})
public class RegisterConfirmController extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String code = request.getParameter("code");
UserDaoService userDaoService = new UserDaoManager();
if (code == null) {
request.setAttribute("info1", "Activation code is not correct!");
request.getRequestDispatcher("error-info").forward(request, response);
} else {
code = MD5.hashedMd5(code);
User user = userDaoService.findByActivationCode(code);
if (user == null) {
request.setAttribute("info1", "Activation code is not correct!");
request.getRequestDispatcher("error-info").forward(request, response);
} else {
LocalDateTime expiredDate = user.getExpiredDate();
LocalDateTime currentDate = LocalDateTime.now();
if (expiredDate.isBefore(currentDate)) {
request.setAttribute("info1", "Activation code is expired!");
request.setAttribute("info2", "resend?id=" + user.getId() + "");
request.getRequestDispatcher("error-info").forward(request, response);
} else if (user.getStatus() == UserStatusEnum.CONFIRMED.getValue()) {
request.setAttribute("info1", "Your account already confirmed!");
request.getRequestDispatcher("error-info").forward(request, response);
} else {
userDaoService.updateStatusById(user.getId(), UserStatusEnum.CONFIRMED);
request.setAttribute("info2", "Your account is confirmed!");
request.getRequestDispatcher("success-info").forward(request, response);
}
}
}
}
3)但问题是当我点击发送到电子邮件的链接时,我收到以下错误。错误 java.time.format.DateTimeParseException: Text '2021-07-24 18:11:33.0' could not be parsed, unparsed text found at index 19
4)这是我与数据库相关的方法。
@Override
public User findByActivationCode(String activationCode) {
try (Connection connection = DbConnection.getConnection()) {
PreparedStatement preparedStatement = connection.prepareStatement("select id,expired_date,status from users where activation_code=?");
preparedStatement.setString(1, activationCode);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
User user = new User();
user.setId(resultSet.getInt(1));
user.setExpiredDate(LocalDateTime.parse(resultSet.getString(2), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
user.setStatus(resultSet.getInt(3));
return user;
}
} catch (Exception e) {
System.out.println("findByActivationCode error " + e);
}
return null;
}