This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes sendError causes IllegalStateException after flushing? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendError causes IllegalStateException after flushing?" Watch "sendError causes IllegalStateException after flushing?" New topic
Author

sendError causes IllegalStateException after flushing?

manoj rajkumar
Greenhorn

Joined: Sep 11, 2006
Posts: 22
Hi, I was going through Professional scwcd book and noted this point:

Statement:
If the ServletResponse.isCommitted() method returns true,
indicating that the response has been committed back to the client
when the sendError() method is called,
it will throw a java.lang.IllegalStateException.

To check this, I typed in the following code snippet in a servlet:

//JAVAJAVAJAVAJAVAJAVAJAVAJAVAJAVAJAVAJAVAJAVAJAVA

PrintWriter out =response.getWriter();
out.println("Hi Manoj");
out.flush();
response.setStatus(500);

//JAVAJAVAJAVAJAVAJAVAJAVAJAVAJAVAJAVAJAVAJAVAJAVA

To my surprise, I did not get any error messages.I got:
Hi Manoj


Can you please explain:
1> Is the statement correct?
2a> If correct, why am I getting the error?
2b> If wrong, when(spec version) was the behavior changed?

I am using jdk 1.5
and apache tomcat 5.5
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

...
If the ServletResponse.isCommitted() method returns true
...
when the sendError() method is called,
...


You didn't do either of these things.
manoj rajkumar
Greenhorn

Joined: Sep 11, 2006
Posts: 22
Hi David,
Does it mean that the response might not yet be committed even if we have the flush statement as above?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

When the response isCommitted(), then sendError() behaves differently than setting a reponse code. So, check isCommitted, then send an error
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: sendError causes IllegalStateException after flushing?