• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Can you write to output stream after forward?

 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Creator of Enthuware JWS+ V6
Posts: 3395
312
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic