aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Good question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Good question" Watch "Good question" New topic
Author

Good question

Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
I'm experimenting a little while preparing to SCWCD and have a question for you. Please answer to yourself without help of your container

Having these two snippets, will they work?



Suchitra Bhat
Ranch Hand

Joined: Dec 30, 2005
Posts: 62
i think the code snippet 1 does not work where as second one works.
Please clarify.
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Thanks for the first answer, let's wait for others to have fun with this question, ok?
shweta bulbule
Ranch Hand

Joined: Mar 24, 2006
Posts: 30
both works fine, in the first case responce is not commited, so will work.
plz answer.


Thanks,<br />Shweta
vandu matcha
Ranch Hand

Joined: Dec 28, 2005
Posts: 57
hi both work fine because in both the cases the response is not committed....
Rajkishore Pujari
Ranch Hand

Joined: Sep 03, 2005
Posts: 46
both will work. They will not work if you add a statement to commit the response using out.flush().
[ April 14, 2006: Message edited by: Rajkishore Pujari ]
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Guys, sorry, but all of you are wrong. Anyone else?
Andy Leung
Greenhorn

Joined: Apr 15, 2006
Posts: 2
I guess the first one is missing a semicolon in line 2 so it doesn't even compile where second one is working with warnings that out is never used but it works.
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
I'm hoping (figures crossed!) this is correct:

I assume the missing semi-colon in line 2 of the first example is a mistake; I'll assume it should be there, and that there are no compilation errors.

Then since a JSP expects a JspWriter (subclass of PrintWriter) to fill its 'out' implicit object, the first example should be fine, but the second will throw an IllegalStateException when the JspPage attemps to invoke response.getWriter() - this is because the getOutputStream() method was already invoked. As many have said, if also the response stream was flushed at any point, that will cause an IllegalStateException with the first method due to the committed response.

I have always found this a particularly difficult issue to resolve (even if it turns out to be an incorrect answer to your question); but then recall that using RD forward should happen as an alternative to writing any content to the response in the calling resource - so in fact, you shouldn't really have obtained any output streams at all in your servlet! This issue is slightly more difficult to justify with includes, but then why would you want to include binary and text data in the same response anyway? So yes, a slightly unsettling issue, but nothing that can't be resolved...

Oh, and by the way, another possible outcome is that a NullPointerException is raised in both if the "result.jsp" resource doesn't exist (since then view is null).

Phew... I think that stuff was just about on the tip of my tongue after spending days and days writing exam-style questions!


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Andy Leung
Greenhorn

Joined: Apr 15, 2006
Posts: 2
What? They are in one servlet? I thought he was talking about two different scenarios.
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Yes, Charles is right. Thank you for explanation. As for missing semicolon in first snippet, it supposed to be there. For those who are more interested, please experiment with writing to the response and closing the stream before forwarding.
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
What? They are in one servlet? I thought he was talking about two different scenarios.


They are two different scenarios - the problem is caused by the fact that JspPage (which all translated JSP pages implement) requires a JspWriter (a PrintWriter) to work, so trying to obtain an OutputStream in the first example then causes an exception. But yes, if you did use these in the same servlet, you would in fact get the same sort of IllegalStateException!
K Thapa
Greenhorn

Joined: Sep 02, 2004
Posts: 8
I am little confused as in why the second one would not work...Although the outputstream has been obtained but they are never committed. So shouldn't that just be fine as long as we dont commit?
Ernesto Leyva
Ranch Hand

Joined: Feb 23, 2006
Posts: 62
I think what charles is saying:

The container constrains you to
call getOutputStream() or getWriter()
but not both. since we are forwarding
this to a JSP after calling the outputstrem
the JSP will call, by default, getWriter()
which breaks the constrain mentioned above.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Good question
 
Similar Threads
Problem regarding to Cookies with EL, HFSJ pg 386
getProperty question
Tomcat's difficulty in evaluating an EL expression
JSTL not working
getRequestDispatcher error in servlets