This article is continuation of email registration, login articles, So please read the previous articles to know how to setup the project. Once you login to the system, you can change password. Now you have have to enter current password and new password. If current password matches, the system will update user's account password with new one. You have to download project at below link for complete understanding. To execute below demo, you need to register first.

Program Flow

  1. User Enter Required details ( Old Password, New Password...etc ) and Submit request to Server
  2. If Old password matches with Users's current password, go to step 3, else go to step 4
  3. Save New Password as User's current password
  4. Inform User that current password didn't match

HTML Code

The below highlighted fields represent current password, new password and confirm password
<form class="form-horizontal" id="formChangePassword" data-toggle="validator" role="form">
    <input type="password" pattern="[A-Za-z0-9@#$%!^&*]{6,30}" name="inputCurrentPassword" class="form-control" id="inputCurrentPassword" placeholder="Enter Current Password" data-error="Password should not be null. It should be greater than 6 and less than 30 characters . Use only A-Z, a-z, 0-9, @ # $ % ! ^ & * charecters" required>
    <input type="password" pattern="[A-Za-z0-9@#$%!^&*]{6,30}" name="inputPassword" class="form-control" id="inputPassword" placeholder="Enter New Password" data-error="Password should not be null. It should be greater than 6 and less than 30 characters . Use only A-Z, a-z, 0-9, @ # $ % ! ^ & * charecters" required>
    <input type="password" name="inputPassword1" class="form-control" id="inputPassword1" data-match="#inputPassword" placeholder="Enter New Password Again" data-error="It should not be null and should match with above password" required>
    <button style="width:100%" type="submit" class="btn btn-default btn-primary">Login</button>
</form>

ChangePassword Servlet Code

Find below code for post method of ChangePassword servlet. This code is responsible for current password verification and updating with new password
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // get current, new password from input and encrypt it 
    String inputCurrentPassword = null;
    if(request.getParameter("inputCurrentPassword")!=null) {
        inputCurrentPassword = BCrypt.hashpw(request.getParameter("inputCurrentPassword"), GlobalConstants.SALT);   
    };
    String inputPassword = null;
    if(request.getParameter("inputPassword")!=null) {
        inputPassword = BCrypt.hashpw(request.getParameter("inputPassword"), GlobalConstants.SALT); 
    };
    
    // get user id from session
    Integer userId = (Integer) request.getSession().getAttribute(GlobalConstants.USER);
    StatusPojo sp = new StatusPojo();
    
    try {
        if(userId!=null && inputCurrentPassword != null) {
            if(UserDAO.verifyUserIdAndPassword(userId.toString(), inputCurrentPassword)) {
                // update password if current password matches
                UserDAO.updatePassword(userId.toString(), inputPassword);
                sp.setCode(0);
                sp.setMessage("Password changed successfully");
            } else {
                sp.setCode(-1);
                sp.setMessage("Current password didn't match");
            }   
        } else {
            sp.setCode(-1);
            sp.setMessage("Invalid input");
        }
    } catch (DBException e) {
        LOGGER.debug(e.getMessage());
        sp.setCode(-1);
        sp.setMessage(e.getMessage());
    }
    PrintWriter pw = response.getWriter();
    pw.write(Utils.toJson(sp));
    pw.flush();
    pw.close();
}

Essential MySQL Queries

// to verify current password
select 1 from DEMO_USER where USER_ID = ? and PASSWORD = ?

// to update password
update DEMO_USER set PASSWORD = ? where USER_ID = ?

6 comments:

  1. please provide this code in spring

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. please send this code inspring

    ReplyDelete
  4. Examcollection 70-532 VCE It’s very excellent information and more real facts to provided that post.Thank you for sharing this information.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Can you give the full code for change password.

    ReplyDelete

Blogroll

Follow this blog by Email

Popular Posts