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

why this piece of code does not throw IllegalStateException ?

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please consider the below code snippet :



when i run this in tomcat 7.0.34 no IllegalStateException is thrown , which it must throw since i'm dispatching after the response has been committed. Please explain ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javadoc: ServletResponse#getWriter()

Returns a PrintWriter object that can send character text to the client. The PrintWriter uses the character encoding returned by getCharacterEncoding(). If the response's character encoding has not been specified as described in getCharacterEncoding (i.e., the method just returns the default value ISO-8859-1), getWriter updates it to ISO-8859-1.

Calling flush() on the PrintWriter commits the response.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply but i did not get you Seetharaman. the code snippet should have thrown IllegalStateException but it DOES NOT(since i'm flushing and then dispatching).
Regards
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just tested in tomcat 7.0.34 (as version you mentioned) , got error :

java.lang.IllegalStateException: Cannot forward after response has been committed


 
Anshuman Chakraborty
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Response has already been commited (out.flush()) and its too late to call request dispatcher.
This is causing IllegalStateException.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:just tested in tomcat 7.0.34 (as version you mentioned) , got error :

java.lang.IllegalStateException: Cannot forward after response has been committed




i tried again number of times but i'm not getting any exception. please help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic