Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setStatus(),sendError() & IllegalStateException

 
suma sindu
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic