File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Directing a servlet to the jsp error page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Directing a servlet to the jsp error page" Watch "Directing a servlet to the jsp error page" New topic

Directing a servlet to the jsp error page

Tareq Shaheen

Joined: Jan 09, 2001
Posts: 5

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?

Tareq Shaheen<BR>JAVA Web App. Developer
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
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 Brown
Author of Professional JSP 2nd Edition
Tareq Shaheen

Joined: Jan 09, 2001
Posts: 5
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.
I agree. Here's the link:
subject: Directing a servlet to the jsp error page
It's not a secret anymore!