0

hi i am new to jsp(apache-tomcat) and i am doing a small project in jsp in that during catch exception i want to redirect some page in jsp and i worte the code like this

try
{
 //some code..
}
catch(Exception e)
{
System.out.println("error==>"+e);
response.sendRedirect("Servererror.html");
}

during error or exception i can see the error by using System.out.println() but the page is not redirecting

is there any problem in my code or am i doing anything wrong...please help..

Pigueiras
  • 18,778
  • 10
  • 64
  • 87
Kumar
  • 355
  • 2
  • 4
  • 15

1 Answers1

1

That is not a good practice that executing the code in catch block.

How ever if you want to show an error page for user,you can specify that in web.xml

 <error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/error.jsp</location>
  </error-page>

If you want to show error pages by error codes (e.g 404,500 etc..or for custom exception handling) please refer this .

And a mini tutorial on this by @BalusC

EDIT:

You need not to do anything with code now.You can simply write

try
{
 //some code..
}
catch(Exception e)
{
System.out.println("error==>"+e);//better to write e.printStacktrace() here.
}

When error occurs the error page automatically shows in browser.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307