Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When an error occurs, you can use RequestDispatcher to forward a request to another resource to hand

 
Parth Twari
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}

 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2275
81
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 2275
81
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ankit Tripathi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 2275
81
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic