Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to return on the same jsp page using servlet

Posted on 2009-12-25
2
Medium Priority
?
822 Views
Last Modified: 2013-11-24
I am having two jsp pages(login and change_password). after the login it done it goes to the change_password page. if any error occurs it shoud return to the same page or it will go to flex application.here if any error occures then it does not return to the same page
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;




	protected void doPost(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		HttpSession session = request.getSession();

		String userName = request.getParameter("userName");
		String oldPassword = request.getParameter("oldPassword");
		String newPassword = request.getParameter("newPassword");
		String reTypeNewPassword = request.getParameter("reTypeNewPassword");

		System.out.println("context path=="+request.getContextPath());
		
		RequestDispatcher dispatcher = request.getRequestDispatcher(request.getContextPath()+ "/ChangePassword.jsp");
		System.out.println("dispatcher value=="+dispatcher.toString());
		
		try {
			
			
		int ctrup=0,ctr1=0,ctr2=0;
	        try
	        {
	        	System.out.println("in updatePassword mathod");
	        		              
	        	//getting the data about the logged in user
	        	Registereduser ru=registeredUserDAO.getUserByUserName(userName);
	        
	        	String currPassword=ru.getUamPassword();
	        	String oldPassword1=ru.getUamOldPassword1();
	        	String oldPassword2=ru.getUamOldPassword2();
	        	String oldPassword3=ru.getUamOldPassword3();
	        		        
	        	Utility util=new Utility();
	        	ErrorWrapper ew=new ErrorWrapper();
	        
	        	//converting the retyped password characters into string
	        	String password1=reTypeNewPassword;
					
	        	//converting date into string format
	        	String dateString = sdf1.format( new Date() );
		

	        	char p='N';

	        	//converting the password characters into string
	        	String p1=oldPassword;

	        	//converting the new password characters into string
	        	String p2=newPassword;

	        	//convering the clear text password into encrypted form
	        	System.out.println("encrypting old password");
	        	String currPwd=util.md5(p1);

	        	//convering the clear text new password into encrypted form
	        	System.out.println("encrypting new password");
	        	String newPwd1=util.md5(p2);

	        	//convering the clear text retyped password into encrypted form
	        	System.out.println("encrypting retyped password");
	        	String reNewPwd=util.md5(password1);
		
	        	Boolean flag=false,flag1=false;
	        	String error="";
	        	String classname="ChangePasswordManager";
	       
	        
	        	//checking if the current password equals the value in the database
	        	System.out.println("checking if old pwd equals curr pwd");
	        	if(currPassword.equals(currPwd))
	        	{
	        		System.out.println("yes check curr pwd equals new pwd");
	        		//checking whether the current password and the newly typed password is same or not?
	        		//if same then display error message
	        		if(currPwd.equals(newPwd1))
	        		{
	        			System.out.println("throw error mesage");
	        			//throw error message
	        			error="E13";
	        			String errorDesc=ew.readErrorDesc(error);
	        			session.setAttribute("errorMessage1", errorDesc);
	        			response.sendRedirect(request.getContextPath()+ "/ChangePassword.jsp");
	        			//dispatcher.forward(request, response);
	        		}
	        		//if the currentpassword and the new typed password are not equal 
	        		else
	        		{
	        			System.out.println("check length of new pwd");
	        			int len=newPassword.length();
	                	                
	        			//checking password length between 8-15 characters
	        			if((len>=8 && len<=15))
	        			{ //start of if2 condition
	        				System.out.println("length proper");
	        								
	        				System.out.println("checking all conditions");
	        					        					//checking if the new password is equal to retyped password
	        					if(newPwd1.equals(reNewPwd))
	        					{
	                            
	        						//start of if4 condition
	        						System.out.println("checking new pwd with all db pwd");
	        						//checking if the new typed password is equal to any of the
	        						//old pasword fields
	        						if(newPwd1.equals(oldPassword1) || newPwd1.equals(oldPassword2) || newPwd1.equals(oldPassword3))
	        						{
	        							System.out.println("new pwd is already used");
	        							flag=true;
	                                
	        						}	
	        						else
	        						{
	        							System.out.println("new pwd is not used");
	        							Date dt=sdf1.parse(dateString);
	        							java.sql.Date dt1=new java.sql.Date(dt.getTime());
	        							System.out.println("in change password p====="+p);
	        							System.out.println("checking if 1 is null");
	        							//if oldpassword1 is blank then insert current password into oldpassword1
	        								response.sendRedirect(request.getContextPath()
	        										+ "/richclient/FlexApplication.jsp");
	        									                                    
	        							}
	                                    
	        							//if oldpassword2 is blank then insert current password into oldpassword2
	        								response.sendRedirect(request.getContextPath()
	        										+ "/richclient/FlexApplication.jsp");
	        									        							}
	        
	        							//if oldpassword3 is blank then insert current password into oldpassword3
	        								        								response.sendRedirect(request.getContextPath()
	        										+ "/richclient/FlexApplication.jsp");
	        								                         
	        							}
	                                        
	        							//if none of he fields are blank then insert oldpassword2 into oldpassword1, oldpassword3 into oldpassword2 and the current password into oldpassword3
	        								response.sendRedirect(request.getContextPath()
	        										+ "/richclient/FlexApplication.jsp");
	        							//	HibernateUtil.commitTransaction();
	        								return;
	                                    
	        							}
	                                
	        						}
	        					}
	        					//if new password and retype password is not same 
	        					else
	        					{
	                            
	        						System.out.println("new and retyped not same");
	        						//throw error message
	        						error="Error";
	        							        						session.setAttribute("errorMessage1", error);
	        						
	        						response.sendRedirect(request.getContextPath()+ "/ChangePassword.jsp");
	        						        					}
	        				}
	        				
	        			// password length is not between 8-15 characters
	        			else
	        			{
	        				System.out.println("pwd length wrong");
	        				//throw error message password length not proper
	        				error="Error";
	        				String errorDesc=ew.readErrorDesc(error);
	        				session.setAttribute("errorMessage1", error);
	        				response.sendRedirect(request.getContextPath()+ "/ChangePassword.jsp");
	        				                        }
	        		}//END OF ELSE PASSWORD/ NEW PWD NOT EQUAL
	        	}//END IF DB PASSWORD=CURR PWD
	        	//current password and the new typed password is same
	        	else
	        	{
	        		System.out.println("new n curr pwd same");
	        		flag1=true;
	        	}
	          
	        	if(flag1==true)
	        	{
	        		//throw error message
	        		error="E06";
	        		String errorDesc=ew.readErrorDesc(error);
	        		session.setAttribute("errorMessage1", errorDesc);
	        		response.sendRedirect(request.getContextPath()+ "/ChangePassword.jsp");
	        		//dispatcher.forward(request, response);
	        	}
	        	if(flag==true)
	        	{
	        		//throw error message
	        		error="Error";
	        		String errorDesc=ew.readErrorDesc(error);
	        		session.setAttribute("errorMessage1", error);
	        		response.sendRedirect(request.getContextPath()+ "/ChangePassword.jsp");
	        		//dispatcher.forward(request, response);
	        	}
	        }  //end of try1 block
	        catch (MyException e1)
	        {
	        	
	          //  session.setAttribute("errorMessage1", "Error Occured in ChangePassword "+e1.getCause().getMessage());
	            response.sendRedirect(request.getContextPath()+ "/ChangePassword.jsp");
	           // dispatcher.forward(request, response);
	        }
		
	        //end of if1 condition*/
	        System.out.println("finished");
	        
	 
			
			
		}
		catch(Exception e1)
		{
			e1.printStackTrace();
			session.setAttribute("errorMessage1", e1.getCause().getMessage());
			dispatcher.forward(request, response);
			
		}	
		
		
		
		
	}

}

Open in new window

0
Comment
Question by:CCBRONET
  • 2
2 Comments
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 2000 total points
ID: 26123841
Use this code.
getServletConfig().getServletContext().getRequestDispatcher("/jsp/changePassword.jsp").forward(request,response);

In the case the error occurs, you need to replace '/jsp/changePassword.jsp' with your change password jsp name, else in case of success, use the login page name

thanks
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26123848
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question