wood burning stoves 2.0*
The moose likes Servlets and the fly likes why this piece of code does not throw IllegalStateException ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "why this piece of code does not throw IllegalStateException ?" Watch "why this piece of code does not throw IllegalStateException ?" New topic
Author

why this piece of code does not throw IllegalStateException ?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Oct 05, 2010
Posts: 20
Response has already been commited (out.flush()) and its too late to call request dispatcher.
This is causing IllegalStateException.

Anshuman Chakraborty
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why this piece of code does not throw IllegalStateException ?
 
Similar Threads
Servlet HelloWorld problem
Html <Head><Title> not showing the title
Java Servlets and JSP pages
tomcat return an error +mvc
PrintWriter no html to response