File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes help on Objective 4.2... 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 » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "help on Objective 4.2..." Watch "help on Objective 4.2..." New topic
Author

help on Objective 4.2...

Vaishali Joshi
Greenhorn

Joined: Dec 28, 2001
Posts: 21
How to use a RequestDispatcher to forward the request to an error page.
Do we do anything different than what we normally do to use a RequestDispatcher?
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
No we don't.
Probably a "trick question"
Tony,SCPJ2, SCWCD


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Vaishali Joshi
Greenhorn

Joined: Dec 28, 2001
Posts: 21
Thanks, Tony. But what happens to the exception that was throw? Do we have to explicitly set javax.servlet.error.* attributes in the request?
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
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


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Vaishali Joshi
Greenhorn

Joined: Dec 28, 2001
Posts: 21
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."
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
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");

------------------
I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Vaishali Joshi
Greenhorn

Joined: Dec 28, 2001
Posts: 21
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help on Objective 4.2...
 
Similar Threads
RequestDispatcher in JSP
RequestDispatcher
sendRedirect
filters applicable for requestDispatcher?
Communication between servlets of two different applications