aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Does setStatus on response object trigger the error page mechanism ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Does setStatus on response object trigger the error page mechanism ?" Watch "Does setStatus on response object trigger the error page mechanism ?" New topic
Author

Does setStatus on response object trigger the error page mechanism ?

Hasnain Javed Khan
Ranch Hand

Joined: Nov 23, 2007
Posts: 73
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.
Hasnain Javed Khan
Ranch Hand

Joined: Nov 23, 2007
Posts: 73
Hello All,

Hasnain Javed Khan wrote:
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.


OKkkaaayyyyyy so Option 2 is incorrect because there is no constructor defined for an int (My Bad ).

As for my second question

Can any one explain how setting the status on the response would trigger the error page mechanism the same way sendError method does ?


does setStatus trigger the error page mechanism ? (I tried it and it does not. Can any one confirm it ?)

Kind Regards.
Hasnain Javed.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

does setStatus trigger the error page mechanism ?

No setStatus is used to send custom response for error/normal response. error page mechanism is only used when you use sendError method...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Hasnain Javed Khan
Ranch Hand

Joined: Nov 23, 2007
Posts: 73
Ankit Garg wrote:No setStatus is used to send custom response for error/normal response. error page mechanism is only used when you use sendError method...


Thanks Ankit

Kind Regards.
Hasnain.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does setStatus on response object trigger the error page mechanism ?