3

I am writing the servlet , in case of exception I am redirecting to my customized error page for that i have done like this.

In web.xml

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

In Servlet,

protected void doPost(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
try{
       //Here is all code stuff
       Throw new Exception();
}catch(Exception e){

         e1.printStackTrace();

}

But here ErrorPage.jsp is not displaying , where I am going wrong can anyone explain me?

Raghu
  • 1,324
  • 7
  • 24
  • 47

3 Answers3

2

You're catching the exception, and only printing the stacktrace inside, so the error-page doesn't take affect, remove the try-catch or re-throw and it will work. In addition, you have some syntax errors. Try something like

try{
       //Here is all code stuff
       throw new Exception();
}catch(Exception e){
         e.printStackTrace();
         throw new ServletException();
}
Master Slave
  • 27,771
  • 4
  • 57
  • 55
  • k . . Thank u , I got the solution & do u know how can I got exception details in `ErrorPage.jsp`? I mean , I wana display error status code. – Raghu Nov 10 '14 at 07:04
1

The problem is that you catch the Exception and therefore no Exception will leave your doPost() method. You will only be redirected error page if an Exception matching the <exception-type> (either identical or a subclass of it) leaves your doPost() method.

You should rethrow the Exception bundled in a RuntimeException for example:

} catch(Exception e) {
    e1.printStackTrace();
    throw new RuntimeException(e);
}

Unfortunately if we're talking about a general Exception you can't just not catch it because doPost() is declared to only throw instances of ServletException or IOException. You are allowed not to catch those, but java.lang.Exception must be caught.

icza
  • 389,944
  • 63
  • 907
  • 827
  • Thank u very much , it worked , & how can I get exception details in my `ErrorPage.jsp`, I mean , I wana display error status code. – Raghu Nov 10 '14 at 06:53
  • @Raghu What do you mean? `/WEB-INF/jsp/ErrorPage.jsp` is yours to edit and make it as custom as you want. – icza Nov 10 '14 at 06:54
  • not much customized , I just wana display error status code. – Raghu Nov 10 '14 at 06:56
  • u got my problem . .? – Raghu Nov 10 '14 at 06:59
  • @Raghu The response status code is available from the `HttpServletResponse` object: `response.getStatus()`. Also note that the `Exception` that triggered the exception page is also available as `exception`, so for example to insert the message of the `Exception`: `<%=exception.getMessage()%>` – icza Nov 10 '14 at 07:09
  • it s exception is undefined in jsp. – Raghu Nov 10 '14 at 07:24
  • @Raghu You have to define `isErrorPage` to `true` at the error.jsp in order to have access to the `exception` like this (you may change other parts, this is just an example): `<%@ page isErrorPage="true" import="java.io.*" contentType="text/html"%>` – icza Nov 10 '14 at 07:32
1

You have handled the Exception in your doPost() using ,

try{
       //Here is all code stuff
       Throw new Exception();
}catch(Exception e){
         e1.printStackTrace();    
}

try and catch blocks. so the errorPage.jsp will not be invoked. <error-page> is invoked for unhandled exceptions

A nice example tutorial Exception Handling

Read for more info Best practice error handling in JSP Servlets

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56