This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
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
Joined: Jan 09, 2001
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.