File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Flushing the response and request dispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Flushing the response and request dispatcher" Watch "Flushing the response and request dispatcher" New topic
Author

Flushing the response and request dispatcher

Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62


Why doesn't this code throw me an IllegalStateException?

Thanks for your help in advance.


SCJP6
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1731
    
  25

Hi Anu,

It should give you something like this:
SEVERE: Servlet.service() for servlet Redir threw exception
java.lang.IllegalStateException: Cannot forward after response has been committed

Have you checked your console window (if you are using Eclipse)?

Don't get cheated by the page in your browser, because that will show an "a". The "a" is sent before the error occurs (the error is not sent to your jsp)

Regards,
Frits

Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62
Hello Frits,

Thanks for the reply.

Frits Walraven wrote:
Don't get cheated by the page in your browser, because that will show an "a".


This is what I am seeing.

Frits Walraven wrote:
The "a" is sent before the error occurs (the error is not sent to your jsp)


What do you mean by "the error is not sent to your JSP"? I have a servlet here. Could you please elaborate? I am a bit confused.

Thanks,
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
Hi.

When flush() is executed, then all what was in buffer is sended to the browser and buffer is cleared.

So, all that is sended with response.getWritter().print("..."); is going to the browser, but first it is in the buffer. When buffer is flushed, things which were in the buffer are being sended to the browser. That's how 'a' gets printed out int he browser, it was sended, becouse the buffer was flushed. So buffer was flushed, it was sended, and in the next line, the statment which gets executed request.getRequestDispatcher("/index.html").forward(request, response); would normally (as I remember) throw an exception and it would be send to the browser, but by that moment the page was allready gone away together with the flush, thats why the exception that would otherwise end on the screen ends nowhere (i think nowhere)... But still, this thing:
SEVERE: Servlet.service() for servlet Redir threw exception
java.lang.IllegalStateException: Cannot forward after response has been committed

That should get printed on the Tomcat's log output (i suppose I can call it log output), anyway in Tomcat's window this error should be observable, as it appears, or, if You work in an Eclipse, in the console widow of the Eclipse (the window down, south..)..


I hope I didn't confused You..

With flush() the page was sent... ( I am not one hundart percent sure, but I think so)

And the error screen that is supposed to be send, ands somewhere in emptiness (if I may say so)...

(But is it possible to send any text else after the flush() and to get it on the screen? )


(the error is not sent to your jsp)

Frits Walraven meant on that "error page"... i suppose (that didn't ended on the screen becaouse the outputput was allready flushed)

I appologize if I was terriblly wrong. I just wanted to slove one question before I g to sleep.

bye
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1731
    
  25

(the error is not sent to your jsp)

What I meant is that it won't be sent to your browser window as the response has already been commited.

Did you check your console window, or if you don't use eclipse check tomcatinstalldir/logs

Regards,
Frits
Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62
Hello Frits,

You are right. I have this message in the log.

SEVERE: Servlet.service() for servlet in context with path threw exception
java.lang.IllegalStateException: Cannot forward after response has been committed


Is this just tomcat specific behavior that it is displaying a part of the page instead of just showing the user the exception report?

Thanks for your help.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Anu Kota wrote:Hello Frits,

Is this just tomcat specific behavior that it is displaying a part of the page instead of just showing the user the exception report?


If we sent the response to client already, then how can you show the exception thrown after the response has been committed?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 317

Thanks Frits


http://www.mahtabalam.net , Oracle Java Programmer , Oracle SQL Expert , Oracle Java Web Component Developer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Flushing the response and request dispatcher