Win a copy of AWS Security this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

forward() Method Problem

 
Ranch Hand
Posts: 47
Netbeans IDE Firefox Browser Java
  • 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.

 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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
Netbeans IDE Firefox Browser Java
  • 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..
 
Ranch Hand
Posts: 73
  • 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
Netbeans IDE Firefox Browser Java
  • 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: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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
Netbeans IDE Firefox Browser Java
  • 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
 
He was expelled for perverse baking experiments. This tiny ad is a model student:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic