You don't use request forwrd to get a jsp error page. To have one, you put the directive < %@ page errorPage="NameOfErrorPage.jsp" %> on any page you want to protect with an error page. Then the error page is a standard jsp with the exception you include the directive < %@ page isErrorPage="true" %> at the top. Once you do this, you have another implicate object "exception" which is of type Throwable. Since Thorwable is the supercalss of all exceptions, it will handle anything thrown during run time. (compile exceptions are not caught) The server implementation handles the method used to transfer control to your error page. It isn't specifyed how so, it's up to the implementers. ------------------ I Hope This Helps Carl Trusiak, SCJP2, SCWCD
Thanks, Carl. My question has nothing to do with JSP. Objective 4 is related to Servlets and in that context, I am not sure what does this mean, "How to use a RequestDispatcher to forward the request to an error page."
posted 18 years ago
OK, Then Tony's answer stands, you don't do anything different than forwarding a request to anthor page and passing an object to it. In your servlet you add req.setAttribute("exception", exception); This will add the exception to the request attributes. The page you forward to, you need to ensure you get the correct type out of the request. I recommend you use the hisghest superclass that will cover all the exceptions you might place in the attributes. Throwable t = (Throwable)request.getAttribute("exception");
Thanks, Carl. If you look at other error/exception handling approaches, the servlet container automatically adds javax.servlet.error.* attributes in the request before sending the request to the error handler. Now, please let me go back to what I said before. My doubt/confusion is : can this be achieved using RequestDispatcher in some way other that the way we normally use it?
The approach that you have given ( setAttribute("exception", e) ) is definitely not as per the standard because in this approach the exception handler has to "know" that it'll get the exception in "exception" attribute instead of javax.servlet.error.exception. Of course, you could set it using this name, but is this what the objective refers to? There are 4-5 other attributes as well that need to be set. Should we set them explicitly? If yes, what's the point of stating this objective explicitly in the error handling section? This is same as when we forward a request to any other jsp/servlet. thanks for your time, Vaishali.
Get me the mayor's office! I need to tell her about this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop