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
staff
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...
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.
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.