Hello All,
I found this question in one of the mock exams from
JDiscuss.
Given the code of doGet() method of a servlet (see exhibit).
The data should be sent to the client only if loginUser() returns a non null userid. Otherwise a status of SC_FORBIDDEN should be sent.
What can be placed at //1 to fulfill this requirement?
Options
Select 3 correct options.
1. req.getRequestDispatcher("errorpage.jsp").dispatch(req, res, HttpServletResponse.SC_FORBIDDEN);
2. throw new ServletException(HttpServletResponse.SC_FORBIDDEN);
3. res.setStatus(HttpServletResponse.SC_FORBIDDEN);
4. res.sendError(HttpServletResponse.SC_FORBIDDEN, "You are not authorized.");
5. res.sendError(HttpServletResponse.SC_FORBIDDEN);
I chose 2,4 and 5 but the correct options shown by JDiscuss are 3,4 and 5.
Can any one explain how setting the status on the response would trigger the error page mechanism the same way sendError method does
?
Thanks in advance.
Kind Regards.
Hasnain Javed.