Forums Register Login

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

+Pie Number of slices to send: Send
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);
}
}

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Sure



i think this is correct usage of dispatcher , i read it on java beat as well.
Java Beat reference

what's your take?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

@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
What's brown and sticky? ... a stick. Or a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3055 times.
Similar Threads
About Forward and Include method in Servlet
difference between request.getRequestDispatcher(),response.sendRedirect()
Exception Handling
How do i set a value in a jsp and get the same value in a servlet?
[HFSJ 2nd Edition] chapter 5 Mock Exam Question # 12
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 20:32:54.