• 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Can you write to output stream after forward?

 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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: 3293
297
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • 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
 
I was born with webbed fish toes. This tiny ad is my only friend:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!