http exception error stack

Hi people,

Im trying to handle an exception in my servlet by res.sendRedirect to another page..but it is not redirecting to the page i want, instead its going to a HTTP Staus 500 error page detailing the full stack trace..can someone tell me what might be wrong here, ive attached code..

Many thanks
oggiemc
import java.io.*;
import java.lang.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

import javax.servlet.http.HttpServletResponse;

import oracle.jdbc.driver.*;

public class Manager2 { 
	
	Connection con = null;
	Statement stmt = null;
	PreparedStatement pstmt;
	ResultSet rs = null;
	String JDBCUrl = "jdbc:xxxxxxxxxxxxxx";
	String username = "xxxx";
	String password = "xxxx";


		
	public void Register(String Username, String Password, 
			String Surname, String Firstname, String Email, String Phone, 
			    PrintWriter out, HttpServletResponse res) throws IOException
	{
              String username = Username;
              String password = Password;
              String surname = Surname;
              String firstname = Firstname;
              String email = Email;
              String phone = Phone;
		
		try
		{
		
		pstmt = con.prepareStatement("INSERT INTO FMC_EMPLOYEE " +
				"(USERID,PASSWORD,SURNAME,FIRSTNAME,EMAIL,PHONE)VALUES(?,?,?,?,?,?)");
		  pstmt.clearParameters();       
		  pstmt.setString(1, username);
		  pstmt.setString(2, password);
		  pstmt.setString(3, surname);
		  pstmt.setString(4, firstname);
		  pstmt.setString(5, email);
		  pstmt.setString(6, phone);
		  pstmt.executeUpdate();
			                                                                                 	   		                                                                                   
		  pstmt.close();

		}
		catch (Exception e) {
		res.sendRedirect("/Fergal/EmployeeRegister.html");
 
	    }
		
	}
}

Open in new window

oggiemcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
do the redirect in your servlet by have the register method throw an exeption and catch it in your servlet

oggiemcAuthor Commented:
Thanks for reply objects,

Not to sure what you mean..Is that not what i have done?

public void Register(String Username, String Password,
            String Surname, String Firstname, String Email, String Phone,
                                     PrintWriter out, HttpServletResponse res) throws IOException
...........
...........
catch (Exception e) {
                res.sendRedirect("/Fergal/EmployeeRegister.html");
Mick BarryJava DeveloperCommented:
no you are redirecting from your Register class.
I am saying you should be doing the redirection in your servlet class
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

oggiemcAuthor Commented:
so the register method throws the exception, but the servlet that callled the method catches it??
how do i do that? do i put the catch statement after the method call (in the servlet)?
Mick BarryJava DeveloperCommented:
yes thats correct

try {
   manager.register(...
} catch (Exception ex) {
   res.sendRedirect("/Fergal/EmployeeRegister.html");
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oggiemcAuthor Commented:
thanx a mill objects!! thats working now..if you dont mind me askin, why couldnt i throw and catch the exception from the register method??
Mick BarryJava DeveloperCommented:
because the servlet would have no idea that it had been redirected and would continue normal processing
oggiemcAuthor Commented:
i see..thanks for your help!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java App Servers

From novice to tech pro — start learning today.