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


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>

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.setMessage("Password changed successfully");
            } else {
                sp.setMessage("Current password didn't match");
        } else {
            sp.setMessage("Invalid input");
    } catch (DBException e) {
    PrintWriter pw = response.getWriter();

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 = ?


  1. please provide this code in spring

  2. This comment has been removed by the author.

  3. please send this code inspring

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

  5. This comment has been removed by the author.

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

  7. I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. Folderlock Regarding PC Download - Avoid Spend Time Browsing, Read Exactly About PC Desktops Right here how to make a folder password protected

  8. Hello, I check your blogs on a regular basis. Your humoristic style is awesome, keep doing what you're doing! Site: How To Lock Files When Nobody Else Will

  9. When it all works as we have come to expect, we are as happy as can be. However, when all does not continue to go as smoothly as we have become accustomed to, the frustration is enormous. reset windows 10 password

  10. Your blog serves as a reminder to practice gratitude daily - thank you! Transform your downtime into an exhilarating clicking adventure with the CPS Test – the ultimate test of speed and accuracy.



Popular Posts