This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forward() Method Problem

 
Kosala W.Abayagunawardene
Ranch Hand
Posts: 47
Firefox Browser Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have run the following servlet(/Forward) service method using request


q1.
even though I have submitted my response to the output before calling forward method "BEFORE forward" is printed.I have learned that submitting to the output before forward will result in IllegalStateException.(ref from Forward)
q2.
submitting response after forward is also given the output of the forwarding servlet not IllegalStateException.


I am using Tomcat 5.5.12 with j2ee 1.4 in netbeans 6.5

Thanks.

 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that the client will see "BEFORE forward" as the output. This is because after writing this to the client, the stream to the client is closed, so when there's an IllegalStateException when you try to forward, the client will have no knowledge of the exception as the output connection (i.e. the print writer) is already closed so information about the IllegalStateException will not be written to the client...
 
Kosala W.Abayagunawardene
Ranch Hand
Posts: 47
Firefox Browser Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:I think that the client will see "BEFORE forward" as the output. This is because after writing this to the client, the stream to the client is closed, so when there's an IllegalStateException when you try to forward, the client will have no knowledge of the exception as the output connection (i.e. the print writer) is already closed so information about the IllegalStateException will not be written to the client...



So if the question was asked whether "What is the output" I should choose "Prints BEFORE forward " right. But enthuware says its IllegalStateException. I am confused..
 
Hasnain Javed Khan
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Found the documentation for method close() of PrintWriter. It does not say anything about flushing what ever is already in the stream.Why don't you run the code and see what happens.
Kind Regards.
Hasnain.

Updating my reply:

I have run the following servlet(/Forward) service method using request


Sorry I missed this part.


Why don't you run the code and see what happens.


Sorry my bad

I hope this thread might clear your confusion as to when the response is actually sent to the client.

Flush() behavior

Hope that helps.

Kind Regards.
Hasnain
 
Kosala W.Abayagunawardene
Ranch Hand
Posts: 47
Firefox Browser Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have run it and the output is mentioned in the question. My problem is IllegalStateException is nerver thrown where is should be thrown...thanks
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kosala W.Abayagunawardene wrote:I have run it and the output is mentioned in the question. My problem is IllegalStateException is nerver thrown where is should be thrown...thanks


As I said, an IllegalStateException will be thrown but you won't be able to see it in the browser. Check the logs of your server and you'll see the exception there...
 
Kosala W.Abayagunawardene
Ranch Hand
Posts: 47
Firefox Browser Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I got what you said Ankit , but what should I pick if a question ask for the output

1. IllegalStateException

or

2. Prints "BEFORE before"

because to my understanding both are correct.Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic