Hi. I have a change password form. In that i have to check the values given in the new password and confirm password are same or not. Please help.
Here is a code that accepts the current password, New password and conform password from the user and change the password.
1)change.jsp
<html> <script> function validate(){ if(document.f.new.value!=document.f.confirm.value){ alert("New Password and Confirm Password should be same! Re-enter confirm-password!"); document.f.confirm.value=""; return false; } return true; } </script> <form name="f" action="changePassword.jsp" method="post" onsubmit="return validate();"> <table> <tr><td>Current Password</td><td><input type="password" name="current" ></td></tr> <tr><td>New Password</td><td><input type="password" name="new"></td></tr> <tr><td>Confirm Password</td><td><input type="password" name="confirm"></td></tr> <tr><td><input type="submit" value="Change Password"></td></tr> </table> </form> </html>
2)changePassword.jsp
<%@page import="java.sql.*"%> <%@page import="java.io.*"%> <% String currentPassword=request.getParameter("current"); String Newpass=request.getParameter("new"); String conpass=request.getParameter("confirm"); String connectionURL = "jdbc:mysql://localhost:3306/test"; Connection con=null; String pass=""; int id=0; try{ Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(connectionURL, "root", "root"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from login where password='"+currentPassword+"'"); if(rs.next()){ id=rs.getInt(1); pass=rs.getString(3); } System.out.println(id+ " "+pass); if(pass.equals(currentPassword)){ Statement st1=con.createStatement(); int i=st1.executeUpdate("update login set password='"+Newpass+"' where id='"+id+"'"); out.println("Password changed successfully"); st1.close(); con.close(); } else{ out.println("Invalid Current Password"); } } catch(Exception e){ out.println(e); } %>
Ads