Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does setStatus on response object trigger the error page mechanism ?

 
Hasnain Javed Khan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Hasnain Javed Khan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic