Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Directing a servlet to the jsp error page

 
Tareq Shaheen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm developing an application with one servlet which directs requests to the right jsp page. If an exception occurs in the servlet, is it possible to have this exception directed to the same page defined by the jsp errorpage directive?
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tareq,
I'm not sure on the best way to do this, but there are a couple of ways that you could try.
(1) If your Servlet is being "included" in a JSP, then by simply throwing an exception, the user will be taken to the standard errorPage that you have defined.
Or if this is not the case,
(2) In your Servlet, you could create a new exception object (don't throw it yet), place it in the session and then redirect/forward to another JSP. This JSP could then find the exception object in the session and throw it, therefore causing the errorPage to be shown.
It sounds a little nasty but should work! ;-)
Another thing that you could look at is defining error-pages in your web.xml file. Here, you can define an error-page for a HTTP error code, or a Java exception type. Unfortunately you have to be specific with the exception class and using an exception-type of java.lang.Exception didn't catch all exceptions in the appserver that I tested this on.
Hope that helps
Simon
------------------
Simon Brown
Author of Professional JSP 2nd Edition
 
Tareq Shaheen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Simon,
I have the business logic in the Servlet which controls JSPs, so solution 2 seems to match my case. The reason I need this it to have the same error page whether an exception is generated in the Servlet or JSP pages.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic