• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sendError(int) vs. setStatus(int)

 
alex lamb
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
This is a real newbie question so my apologies upfront for any major misunderstandings. What is sendError(int) doing from setStatus(int) that makes it different ?
My understanding is that setStatus simply sets the error code on the response object and then web server will return some default message/page that is simply displayed by the client. right ?
Regarding sendError(int), am I to understand that it sets the status code but in addition to that, sets up the content body and hence potentially throws an IOException ? What is in this content body that we are not specifying and where does it come from ?
Also, on a related note, why does the buffer need to get flushed when sendError(int) is called ?
Many thanks for your answers !
Alex
 
L Goundalkar
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..
setStatus(int StatusCode) sets the status for the response. what u have in your mind is correct.
For sendError(int StatusCode) sends the error response to the client directly. After this you cent send response to client.
Regarding the buffer, I think its not Flusing.. its clearing.. i.e. when you call sendError(int) the error is send to client and the contents in the buffer are CLEARED. i.e. removed.
Correct me if I am wrong.
cheers.
 
Kevin Postlewaite
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the generated API documentation:
setStatus
"
public void setStatus(int sc)
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.
"
So there are times when you need to set the status when there is no error and thus should not use sendError().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic