aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes forward() Method Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "forward() Method Problem" Watch "forward() Method Problem" New topic
Author

forward() Method Problem

Kosala W.Abayagunawardene
Ranch Hand

Joined: Dec 15, 2007
Posts: 47

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.



[SCJP] ::[SCWCD]::[BCS::BIT::SCBCD] - Studying[My Blog]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Kosala W.Abayagunawardene
Ranch Hand

Joined: Dec 15, 2007
Posts: 47

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

Joined: Nov 23, 2007
Posts: 73
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

Joined: Dec 15, 2007
Posts: 47

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

Joined: Aug 03, 2008
Posts: 9302
    
  17

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

Joined: Dec 15, 2007
Posts: 47

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
 
subject: forward() Method Problem