| Author |
When an error occurs, you can use RequestDispatcher to forward a request to another resource to hand
|
Parth Twari
Ranch Hand
Joined: Jan 20, 2010
Posts: 163
|
|
RequestDispatcher can be used for error handling in a web application?
true\false
Is this paragraph right?
Source: Whizlab simulator
RequestDispatcher
When an error occurs, you can use RequestDispatcher to forward a request to another resource to handle the error. The error attributes can be set in the request before it is dispatched to the error page, as shown below:
public void doGet(HttpServletRequest req, HttpServletResponse res){
try {
// Code that throws exception
}
catch (Exception ex) {
request.setAttribute("javax.servlet.error.exception", ex.getMessage());
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("error.jsp");
rd.forward(request,response);
}
}
|
Parth Tiwari
| Pursuing Bachelor of Engineering | OSUM Club Leader | SCJP 6 | SCWCD 5 |...
|
 |
Frits Walraven
Rancher
Joined: Apr 07, 2010
Posts: 1043
|
|
Hi Marcus,
Please use code tags otherwise it becomes difficult to read. (you can still use the Edit button, and put in the code tags by pressing the Code button)
What do you think is the correct answer?
Regards,
Frits
|
 |
Parth Twari
Ranch Hand
Joined: Jan 20, 2010
Posts: 163
|
|
Sure
i think this is correct usage of dispatcher , i read it on java beat as well.
Java Beat reference
what's your take?
|
 |
Frits Walraven
Rancher
Joined: Apr 07, 2010
Posts: 1043
|
|
Hi Marcus,
There is one small mistake in the code and that is the pathname given to the RequestDispatcher: it has to start with a "/".
Regards,
Frits
|
 |
Parth Twari
Ranch Hand
Joined: Jan 20, 2010
Posts: 163
|
|
ya right right
thats because we are using ServletContext to get the request dispatcher and not the request.
But otherwise code looks right specially because we got it from java beat right?
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
The given code will work, but it doesn't replace the built-in error handling mechanism of a servlet container. The servlet container sets 6 attributes in the request which the error page can use, but this code sets only one of them. Other than that the code will work fine...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Ankit Tripathi
Ranch Hand
Joined: Oct 17, 2009
Posts: 175
|
|
Yes.
But it is not only
error handling
for which RequestDispatcher interface works.
A RequestDispatcher object can forward a client's request to a resource or include the resource itself in the response back to the client. A resource can be another servlet, or an HTML file, or a JSP file.
|
 |
Parth Twari
Ranch Hand
Joined: Jan 20, 2010
Posts: 163
|
|
got this in spec which says
@ankit garg - here the spec is asking the container to set the attributes or us to manually set?
or rather both?
|
 |
Frits Walraven
Rancher
Joined: Apr 07, 2010
Posts: 1043
|
|
@ankit garg - here the spec is asking the container to set the attributes or us to manually set?
These attributes are set by the container
Regards,
Frits
|
 |
 |
|
|
subject: When an error occurs, you can use RequestDispatcher to forward a request to another resource to hand
|
|
|