Change Password Code in JSP

In this example we will see how to change password code in jsp. First of all we have created a form where we have displayed three password fields "oldpassword","newpassword","confirmpassword" and a Submit Form button.

Change Password Code in JSP

Change Password Code in JSP

In this example we will see how to change password code in jsp. First of all we have created a form where we have displayed three password fields "oldpassword","newpassword","confirmpassword" and a Submit Form button. We have used JavaScript validation in Changepassword.jsp form. If you will not enter value in password field than an error message will be shown and it will ask you to insert password. If you enter correct OldPassword but the NewPassword does not match with the ConfirmPassword, an error message will be shown saying that the password does not match. And if you enter wrong OldPassword but correct NewPassword and ConfirmPassword, at the time of submit it will display that the OldPassword does not match.

OldPassword is already saved in the database and when a user enters the OldPasswrod, the program checks it with the database and see if it matches or not. In the new password, it matches the password entered in the first field with the password entered in the second field and if both of them matches, it saves the password in the data as the new password.

In this example we have made entry in every field mandatory hence an error is shown if one of them is not filled correctly.

We have a created database and we create password field in fields which you can copy and use it:

                  CREATE TABLE `changepassword` (                
                  `id` int(10) NOT NULL,                       
                  `oldpassword` varchar(10) DEFAULT NULL,      
                    PRIMARY KEY (`id`)                           
                ) ENGINE=InnoDB DEFAULT CHARSET=latin1         

Description of program:

In this example we have used JSP method and MySQL Query. we use request.getParameter method. The request.getParameter method of request object in JSP (java server page) application and getting values. we use some database query executeQuery(String MySQL) and execute Update(String MySQL).

  • executeQuery(String MySQL) :- The executeQuery(String MySQL) is a return of Boolean value right if a ResultSet object can be retrieved and otherwise return false.
  • executeUpdate(String MySQL):-The executeUpdate(String MySQL) is a return thr no or rows and colume updated in database during the execution of the MySQL statement.

Syntax of request.getParameter.

String string = request.getParameter("");

ChangePasswordForm.jsp

<html>
<head>
<title>Change password in jsp</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<h2 align="center"><strong>Change password in JSP form</strong></h2>
<body>
<script language="javascript">
function fncSubmit()
{

if(document.ChangePasswordForm.OldPassword.value == "")
{
alert('Please input old password');
document.ChangePasswordForm.OldPassword.focus();
return false;
} 

if(document.ChangePasswordForm.newpassword.value == "")
{
alert('Please input Password');
document.ChangePasswordForm.newpassword.focus(); 
return false;
} 

if(document.ChangePasswordForm.conpassword.value == "")
{
alert('Please input Confirm Password');
document.ChangePasswordForm.conpassword.focus(); 
return false;
} 

if(document.ChangePasswordForm.newpassword.value != document.ChangePasswordForm.conpassword.value)
{
alert('Confirm Password Not Match');
document.ChangePasswordForm.conpassword.focus(); 
return false;
} 

document.ChangePasswordForm.submit();
}
</script>
<form name="ChangePasswordForm" method="post" action="processChangePWD.jsp" OnSubmit="return fncSubmit();">

<table border="1" align="center" bgcolor="#2B60DE">

<tr>
<td>OLD PASSWORD</td>
<TD><input name="OldPassword" type="password" id="OLDpwd" size="20"></td>
</tr>
<tr>
<td>NewPassword</td>
<td><input name="newpassword" type="password" id="newpassword">
</td>
</tr>
<tr>
<td>Confirm Password</td>
<td><input name="conpassword" type="password" id="conpassword">
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Save"></td>
</tr>

</table>
</form>
</body>
</html>

ProcessChangePWD.jsp

<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<%
String OldPassword = request.getParameter("OldPassword");
String Newpass = request.getParameter("newpassword");
String conpass = request.getParameter("conpassword");


Connection con = null;
Statement st = null;
String pass = "";
int id = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/naulej";
con = DriverManager.getConnection(url, "root", "root");
st = con.createStatement();
ResultSet rs = st.executeQuery("select * from changepassword where oldpassword= '"+ OldPassword + "'");
if (rs.next()) { 
pass = rs.getString("oldpassword");
} 
if(Newpass.equals(conpass))
{
if (pass.equals(OldPassword)) {
st = con.createStatement();
int i = st.executeUpdate("update changepassword set oldpassword='"+ Newpass + "'");
out.println("Password changed successfully");
st.close();
con.close();
} else {
out.println("Old Password doesn't match");
}
/*}else{
out.println("new password and confirm new password is not matching");
}*/
}
} catch (Exception e) {
out.println(e);
}
%>

WebContent.rar

Download Source Code