Computer Associates Placement: Sample Questions 4 - 4 of 18

Glide to success with Doorsteptutor material for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 4

Appeared in Year: 2004

Question

MCQ▾

How can a Servlet call a JSP error page?

Choices

Choice (4)

a.

The servlet needs to forward the request to the specific error page URL. The exception is passed along as an attribute named “javax. Servlet. Jsp. JspException”

b.

When the servlet throws the exception, it will automatically be caught by the calling JSP page.

c.

The servlet needs to redirect the response to the specific error page, saving the exception off in a cookie.

d.

This capability is not supported.

Answer

a.

Explanation

  • If the source of the exception was a JSP page, the Web container also stores the exception as a request attribute called “javax. servlet. jsp. jspException”
  • So if you want from Servlet to JSP-Error-Page then you have to set the exception in the same way as the Web Container does i.e. in the catch block you set: request. setAttribute ( “javax. servlet. jsp. jspException” ) ;
  • Then use RequestDispatcher to forwrd to your error page.

Program:

  1. PublicvoiddoGet(HttpServletRequestrequest, HttpServletResponseresponse) throwsServletException, IOException
  2. {
  3.     Catch (Throwablee)
  4.     {
  5.         request.setAttribute(“javax.servlet.jsp. jspException”, e);
  6.         RequestDispatcherdispatcher =request.getRequestDispatcher("errorPage.jsp");
  7.         dispatcher.forward(request, response);
  8.     }
  9. }

Developed by: