This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet response already use stream, Writer not possible

 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to emit HTML and an image from your JSP?
 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abiodun Adisa:
and it worked well

Most excellent!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic