• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}

 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic