• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sybex chap 5 question 1

 
friso jonge
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which of the following calls will cause an error page to be automatically generated by the server ?
1) resp.setStatus(404);
2) resp.setStatus(request.SC_NOT_FOUND);
3) resp.sendError(resp.SC_NOT_FOUND);
4) resp.sendError(404,"could not find file");
the setStatus method mentions something which need some clearing up...
If you use setStatus to define an error, then the servlet is completely responsible for generating the response. If an error page is not configured an server-dependant PAge not Found will appear.
So is n't A a right answer as well ? (even though you should not use it to define errors)
Btw, the book mentions right answer as C and D
again any ideas ?
thanks,
friso
 
k space
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The description of setStatus() says:
Sets the status code for this response. This method is used to set the return status code when there is no error (for example, for the status codes SC_OK or SC_MOVED_TEMPORARILY). If there is an error, and the caller wishes to invoke an defined in the web applicaion, the sendError method should be used instead.
The container clears the buffer and sets the Location header, preserving cookies and other headers.
That means, it won't generate any error page as a result of calling setStatus, but sendError does.
 
friso jonge
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
true what you say is in documentation...
however the sybex book also mentions:
If you use setStatus to define an error, then the servlet is completely responsible for generating the response. If an error page is not configured an server-dependant PAge not Found will appear.
I agree completely that you should use sendError for errors, but that was not the question. Anyway i tested it mself and indeed it does not generate an error page.
 
k space
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The sybex book has lots of errors, don't trust the book too much. If you have any doubt, always refer to the spec.
I have that book and found the good thing of it is its errors. Through this error discovery process, I understand more than remembering everything just for the exam.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic