Forgot Password Action
Forgot Password Action
The password forgot Action is invoked when you forgot your password and want to recover the password. The
forgot password action requires user name and passwords same as you had entered
during registration. This given values are matched from the database in the
forgot password action, if the value matches the action sends an email of
user name and passwords to the user email account.
The development process of ForgotPasswordAction is as follows.
At first get the entered data (User Name & Email Address) from the forgot password form
UserForgetPasswordForm forgetform = (UserForgetPasswordForm) form; String strUserName=forgetform.getUsername(); String strEmail=forgetform.getEmail();
Then retrive a DAO(Data Access Object Reference) and get the user deatail from the database and compare it
roseindia.dao.SpringHibernateDAO springHibernateDAO = (roseindia.dao.SpringHibernateDAO) ServiceFinder.getContext(request) .getBean("SpringHibernateDao"); String[] strPasswordEmail = springHibernateDAO.retriveUserForgetPassword(strUserName,strEmail);
If comparison is correct then add the user detail and send an email.....
String [] reciepent={strPasswordEmail[1]}; String username=strPasswordEmail[2]; String subject="Your username & password "; String message="Hi,"+username; message+="\n Your username is "+username+"."; message+="\n Your password is "+strPasswordEmail[0]+"."; message+="\n Please login to the web site with your username and password."; message+="\n \n Thanks"; message+="\n \n \n Regards"; String from=roseindia.web.common.ProjectConstants.FROM_MAIL; mailBean.sendMail(reciepent,subject,message,from);
The complete code of forgot password action is given below
UserForgetPasswordAction.java
package roseindia.web.struts.action; import roseindia.web.struts.form.UserForgetPasswordForm; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessages; import org.apache.struts.action.ActionMessage; import roseindia.services.ServiceFinder; public class UserForgetPasswordAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{ //Retrieve the DAO Reference roseindia.dao.SpringHibernateDAO springHibernateDAO = (roseindia.dao.SpringHibernateDAO) ServiceFinder.getContext(request) .getBean("SpringHibernateDao"); // Retrieve the Mail Bean Reference roseindia.web.common.SendMail mailBean = (roseindia.web.common.SendMail) ServiceFinder.getContext(request).getBean(roseindia.web.common.ProjectConstants.MAIL_BEAN); // Create object of ActionMesssages ActionMessages errors = new ActionMessages(); UserForgetPasswordForm forgetform = (UserForgetPasswordForm) form; String strUserName=forgetform.getUsername(); String strEmail=forgetform.getEmail(); String[] strPasswordEmail = springHibernateDAO.retriveUserForgetPassword(strUserName,strEmail); if(strPasswordEmail[0] != null && strPasswordEmail[1] != null){ System.out.println("Password:"+strPasswordEmail[0]); System.out.println("email:"+strPasswordEmail[1]); //sending mail String [] reciepent={strPasswordEmail[1]}; String username=strPasswordEmail[2]; String subject="Your username & password "; String message="Hi,"+username; message+="\n Your username is "+username+"."; message+="\n Your password is "+strPasswordEmail[0]+"."; message+="\n Please login to the web site with your username and password."; message+="\n \n Thanks"; message+="\n \n \n Regards"; // getting from emailid from allcooljobs.web.common.CollJobsConstants String from=roseindia.web.common.ProjectConstants.FROM_MAIL; try{ mailBean.sendMail(reciepent,subject,message,from); }catch(Exception e){ System.out.println("Error in sending mail:"+e); } return mapping.findForward("success"); }else{ errors.add("invalid",new ActionMessage("error.usernameEmail.invalid")); saveErrors(request,errors); return mapping.findForward("failure"); } } }