File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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
Author

Directing a servlet to the jsp error page

Tareq Shaheen
Greenhorn

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
    
    6
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

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: http://aspose.com/file-tools
 
subject: Directing a servlet to the jsp error page
 
Similar Threads
servlet call jsp error page
Query related to Error Pages
JSP to Servlet call
Servlet Response
Server Error