I have created a website where the user will login in order to have access to the site. The user has the option to change the
password that he/she has.The jsp page has 4 textboxes where the user must complete in order to change the password. To complete the user name,old password,new password and confirm the new password. I have created a JavaBean where i have a method called UpdateChange that returns boolean. This method checks whether the user has put correct the user name and password comparing with the database and also if the new password is equal with the confirm password. If the user has complete correct the textboxes then it returns true and the update has been done.Otherwise it returns false. In the jsp page now i have make a refernece to the the bean that i created and i call the method UpdateChange and if it is true then it displays the following message:"You have change your password" else display "You didn't change your password". This function works fine, it changes password when the user has put correct information and deny to change the password if the user had made a mistake. The problem is that when the jsp page opens for the first time before the user complete any textboxes it appears to the screen the message "You have change your password".
Can you please help to fix this problem?
I am also presented the code that i have already written:
JavaBean:
public boolean UpdatePassword(){
boolean update=false;
String newpass="";
String user="";
String oldpass="";
try{
oldPassword=getOldPassword
();
newPassword=getNewPassword
();
confirmPassword=getConfirm
Password()
;
userName=getUserName();
conn=DriverManager.getConn
ection("jd
bc:mysql:/
/localhost
:3306/test
2", "xenia", "");
String query="SELECT user_name,password FROM users where user_name='"+ userName+"'";
System.out.println("Query:
"+ query);
qstmt=conn.createStatement
();
rs=qstmt.executeQuery(quer
y);
while(rs.next()){
user=rs.getString(1);
oldpass=rs.getString(2);
System.out.println(""+ user);
System.out.println(""+ oldpass);
}
String updatequery="UPDATE users SET password='"+ newPassword+"'WHERE password='"+ oldPassword+"' and user_name='"+ userName+"'" ;
stmt=conn.createStatement(
);
if ((user.equals(userName))&&
(oldpass.equals(oldPasswor
d))&& (newPassword.equals(confir
mPassword)
)) {
int rows=stmt.executeUpdate(up
datequery)
;
update=true;
System.out.println("mesa sto if statement " + userName + rows);
}
}
catch (Exception e){
System.out.println("SQL Exception is caught");
}
return update;
}
JSP:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%! public final static String FAILPASS="You failed to change your password,please try again";
public final static String CHANGEPASS="You have successfully change your password!";
public final static String PASSDEFAULT=" YOU HAVE TO ENTER The CORRECT ITEMS TO CHANGE YOUR PASSWORD";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"
http://www.w3.org/TR/html4/loose.dtd">
<%-- define the bean reference--%>
<jsp:useBean id="changePass" scope="request" class="security.login.Chan
gePassword
"/>
<jsp:setProperty name="changePass" property="userName" param="user"/>
<jsp:setProperty name="changePass" property="oldPassword" param="old"/>
<jsp:setProperty name="changePass" property="newPassword" param="newp"/>
<jsp:setProperty name="changePass" property="confirmPassword"
param="confirm"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Change the password</title>
</head>
<body>
<h1>Change password</h1>
<table border="0" cellpadding="0" cellspacing="0">
<form name="update" action="" method="post">
<tr>
<td> User Name:</td>
<td><input type="text" name="user" size="18"></td>
</tr>
<tr>
<td>
Old Password:
</td>
<td>
<input type="password" name="old" size="20"></td>
<tr>
<td>
New Password: </td>
<br>
<td>
<input type="password" name="newp" size="20"></td>
</tr>
<br>
<tr>
<td>Confirm new password: </td>
<td> <input type="password" name="confirm" size="20"></td>
<td></td>
<br>
<td></td>
<td>
<input type="submit" name="Submit" value="Change Password" size="20" ></td>
</tr>
</table>
</form>
<% String oldPassword=changePass.get
OldPasswor
d();
String newPassword=changePass.get
NewPasswor
d();
String confirmPassword=changePass
.getConfir
mPassword(
);
String userName=changePass.getUse
rName();
boolean update=changePass.UpdatePa
ssword();
if (update==true) { %>
<font color="blue"> <%= CHANGEPASS %>
<% }else
{ %>
<font color="red"> <%= FAILPASS%>
<% }
%>
</body>
</html>
Thank you very much in advance.
Start Free Trial