• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Good question

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?



 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think the code snippet 1 does not work where as second one works.
Please clarify.
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the first answer, let's wait for others to have fun with this question, ok?
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
both works fine, in the first case responce is not commited, so will work.
plz answer.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi both work fine because in both the cases the response is not committed....
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys, sorry, but all of you are wrong. Anyone else?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Andy Leung
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What? They are in one servlet? I thought he was talking about two different scenarios.
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic