aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes When an error occurs, you can use RequestDispatcher to forward a request to another resource to hand Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "When an error occurs, you can use RequestDispatcher to forward a request to another resource to hand" Watch "When an error occurs, you can use RequestDispatcher to forward a request to another resource to hand" New topic
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
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

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
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

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
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

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: 198
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

The request path and attributes are set as if a RequestDispatcher.forward to
the error resource had been performed.
The request attributes in Table SRV.9-1 must be set.

javax.servlet.error.status_code java.lang.Integer
javax.servlet.error.exception_type java.lang.Class
javax.servlet.error.message java.lang.String
javax.servlet.error.exception java.lang.Throwable
javax.servlet.error.request_uri java.lang.String
javax.servlet.error.servlet_name java.lang.String


@ankit garg - here the spec is asking the container to set the attributes or us to manually set?
or rather both?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

@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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When an error occurs, you can use RequestDispatcher to forward a request to another resource to hand