2

I have a JSP page with an error page specified. I call the page from a servlet via:

RequestDispatcher rd = ctx.getRequestDispatcher( jspPage );
rd.include( req, res );

How can I detect if the jsp page forwarded to the error page or not? I want to handle exceptions differently but no exception is thrown. And unfortunately I can't modify the JSP page itself or the error page.

Edit:

I'm thinking something like this might work after the include() line, any thoughts?

Object errorServletName = req.getAttribute( "javax.servlet.error.servlet_name" );
if ( errorServletName != null ) 
{ there was an error in the JSP... }
Art Peterson
  • 569
  • 2
  • 6
  • 14
  • Where exactly do you want to detect this? After the `include()` line in the given example? And why does your JSP throw exceptions? This indicates using [scriptlets...](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files) Do you realize that it may be too late then to display the error page? I.e. when it throws exception while the response is already been committed. Anyway, are you allowed to change the `` entry in `web.xml`? If so, then that's the answer. – BalusC Jan 05 '11 at 19:32
  • I want to detect it after the include() line in the example. The JSP page has an errorPage specified so it gets kicked over to error.jsp when there is an exception, which is normally fine. But after the include() line I need to do a res.getContents() and do other processing with the output...and I need to handle the error page output as a special case, so I'm hoping there is a way to detect whether or not it was forwarded to the errorpage. Hope that clarifies... – Art Peterson Jan 05 '11 at 19:49

1 Answers1

0

Add to your web.xml file this:

<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/error.html</location>
</error-page>

Assuming that you have stack trace printout or similar way of notifying the user about there problem they experienced on error.html page.

MatBanik
  • 26,356
  • 39
  • 116
  • 178