• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

help on Objective 4.2...

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No we don't.
Probably a "trick question"
Tony,SCPJ2, SCWCD
 
Vaishali Joshi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Tony. But what happens to the exception that was throw? Do we have to explicitly set javax.servlet.error.* attributes in the request?
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vaishali Joshi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic