GeeCON Prague 2014*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes setStatus(),sendError() & IllegalStateException 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 "setStatus(),sendError() & IllegalStateException" Watch "setStatus(),sendError() & IllegalStateException" New topic
Author

setStatus(),sendError() & IllegalStateException

suma sindu
Ranch Hand

Joined: Apr 14, 2005
Posts: 35
Hi

I'm sorry for not trying out these and asking others questions.

in IBM site, i found that

These methods(setStatus() and sendError() ) throw an IllegalStateException if the response is already committed.


but j2eecertificate.com says
" setStatus(int sc) method is not declared to throw an IllegalStateException" where as sendError() can throw IllegalStateException.


which statement about setStatus() is correct?


Thanks
Suma
chowdary Thammineedi
Ranch Hand

Joined: Aug 16, 2004
Posts: 126
Hai Suma

IBM IS WRONG.(By what you said)

setStatus in the javadocs 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 error-page defined in the web application, the sendError method should be used instead.
The container clears the buffer and sets the Location header, preserving cookies and other headers. "

Remember setStatus() is used to set a header when there is NO ERROR.

sendError() in HttpServletResponse is defined as follows

public void sendError(int sc,
java.lang.String msg)
throws java.io.IOException

OR

public void sendError(int sc)
throws java.io.IOException

Both these methods say this

"If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to. "

SO Finally
what j2eecertificate.com says is RIGHT.

Neither setStatus() nor sendError() are declared to throw an IllegalStateException.

setStatus() is NOT declared to throw ANY exception.(Just sets a header)

sendError() is declared to throw an IOException, But it can also throw an IleegalStateException if the response has already been commited before calling this method.

setStatus() sets a header value.

sendError() sends a FINAL response to the client.

5 More days to go.(For your exam that is) Isn't it? All the best in your EXAM CRAM.
suma sindu
Ranch Hand

Joined: Apr 14, 2005
Posts: 35
Thanks a lot chowdary for ur quick replies.

Wow, u remembered my exam date(which I wrote about few days ago).

Actually in the last minute, I had to prepone my exam date. I'm writing my exam tomorrow, but i'm not sure if I'm ready yet.


so, little bit nervous(u know how valuable last few days are, and i'm missing those last minute revisions of what I thought I should read before the exam).

Suma
chowdary Thammineedi
Ranch Hand

Joined: Aug 16, 2004
Posts: 126
Don't worry. You'll do WELL.

Which part of the planet are you on?

If you are in the US, better go to bed. It's 1.50 AM Eastern Time.
 
 
subject: setStatus(),sendError() & IllegalStateException