Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Flushing the response and request dispatcher

 
Anup Om
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why doesn't this code throw me an IllegalStateException?

Thanks for your help in advance.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2409
95
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 2409
95
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Frits
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic