aspose file tools*
The moose likes JSP and the fly likes Servlet response already use stream, Writer not possible Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Servlet response already use stream, Writer not possible" Watch "Servlet response already use stream, Writer not possible" New topic
Author

Servlet response already use stream, Writer not possible

Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
I am trying to obtain an image byte from the Session Context, Then i would like to display this image, The code segment is below



But unfortunately i am getting the following exception



Please how can i resolve this
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42906
    
  69
Where is this code located - in a JSP page? I'm guessing it is, since you are posting to the JSP forum. A JSP page really can't (and shouldn't) use the output stream. If the response consists of an image you need move this code to a servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61747
    
  67

Are you trying to emit HTML and an image from your JSP?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
Originally posted by Bear Bibeault:
Are you trying to emit HTML and an image from your JSP?


yes Sir thats what i am trying to do
Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
Originally posted by Abiodun Adisa:


yes Sir thats what i am trying to do


yes Sir thats what i am trying to do , I am trying to emit an image
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61747
    
  67

Originally posted by Abiodun Adisa:
I am trying to emit an image

Remember that a JSP is merely a template to deliver an HTML page to the browser. (See
this article for details).

And to include an image in an HTML page, you don't embed image data, do you? No, you reference the image with an <img> tag.

So you need to do the same thing in your JSP. Include an <img> tag just as if it was an HTML page, specifying the URL of a servlet as the src of the <img> tag, that will feed up the image data.
Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
Originally posted by Bear Bibeault:

Remember that a JSP is merely a template to deliver an HTML page to the browser. (See
this article for details).

And to include an image in an HTML page, you don't embed image data, do you? No, you reference the image with an <img> tag.

So you need to do the same thing in your JSP. Include an <img> tag just as if it was an HTML page, specifying the URL of a servlet as the src of the <img> tag, that will feed up the image data.


Actually I am using JSF I have this Tag


and it references another JSP page which is
fileupload_showimg.faces and this contains the code below


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61747
    
  67

Originally posted by Abiodun Adisa:
and it references another JSP page

This is folly. JSP pages should never be used to serve non-character data. Never. Ever. Ever.

Did you read the article I linked to?

Your image serving code should be a servlet. Plain and simple.
[ December 15, 2007: Message edited by: Bear Bibeault ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

JSP compilers will often add characters to the output stream going to the browser. Since the primary intent for JSP is textual output (Mainly HTML or XML) these characters aren't a problem. If you're trying to write binary data to a browser, however, they will become a problem. This is why it's best to do this with a servlet.

We have an example app in our codebarn that streams images to a browser.
See: http://faq.javaranch.com/java/CodeBarnSimpleStream


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
Thanks folks ,I used a servlet and it worked well. The code i earlier tried was straight from the myfaces example i downloaded from Myfaces. its contained
in the file_upload.java file and it used a JSP to emit images which is wrong like
you two gentlemen pointed out and this i agree with

Thank you
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61747
    
  67

Originally posted by Abiodun Adisa:
and it worked well

Most excellent!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet response already use stream, Writer not possible