Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sendError causes IllegalStateException after flushing?

 
manoj rajkumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...
If the ServletResponse.isCommitted() method returns true
...
when the sendError() method is called,
...


You didn't do either of these things.
 
manoj rajkumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the response isCommitted(), then sendError() behaves differently than setting a reponse code. So, check isCommitted, then send an error
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic