• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Does setStatus on response object trigger the error page mechanism ?

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
bacon. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic