aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Can you write to output stream after forward? 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 "Can you write to output stream after forward?" Watch "Can you write to output stream after forward?" New topic
Author

Can you write to output stream after forward?

Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Another question from Niko's:

a. An IllegalStateException is thrown at runtime because response.getWriter() is called more than once.
b. An IllegalStateException is thrown at runtime at line #1 because the request is dispatched after writing data.
c. An IllegalStateException is thrown at runtime at line #2 because data is written after the request has been dispatched.
d. An IllegalStateException is thrown at runtime at line #3 because the writer is closed after the request has been dispatched.
e. The browser displays the content of hello.jsp without any exception at runtime.
f. The browser displays aa without any exception at runtime.
g. The browser displays a without any exception at runtime.


I thought an exception will occur at line 2 or 3, since you try to write to & close (which will flush) the out object after forwarding through request dispatcher, but the answer given is e.

Can someone please explain this to me?


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

I guess that PrintWriter is not flushing the buffer if you don't use println(-), printf(-) or format(-) methods.

No flush == than no real changes to the output stream, so redirection can be executed.

Take a look at JDK for PrintWriter: http://download.oracle.com/javase/6/docs/api/java/io/PrintWriter.html

Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output. These methods use the platform's own notion of line separator rather than the newline character.


Edit: Oh, and in my opinion, the closing of the stream will not occur at all, as it's forward operation - not an include one. It dispatches the whole request rather than dispatch and continue the main flow.


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1718
    
  25

Edit: Oh, and in my opinion, the closing of the stream will not occur at all, as it's forward operation - not an include one. It dispatches the whole request rather than dispatch and continue the main flow.

Correct, the application completely ignores the lines after a forward.

Regards,
Frits
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can you write to output stream after forward?