File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

setStatus(),sendError() & IllegalStateException

suma sindu
Ranch Hand

Joined: Apr 14, 2005
Posts: 35

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 says
" setStatus(int sc) method is not declared to throw an IllegalStateException" where as sendError() can throw IllegalStateException.

which statement about setStatus() is correct?

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)


public void sendError(int sc)

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 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).

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.
I agree. Here's the link:
subject: setStatus(),sendError() & IllegalStateException
It's not a secret anymore!