• Post Reply Bookmark Topic Watch Topic
  • New Topic

Display an image on JSP from byte[]

 
Carl Jenkins
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a byte[] of an image stored in session .

I'd like to be able to display (write out) the byte[] to the JSP i am going to.

My main JSP has an included jsp which gets the byte[] to write out. But, I keep getting an IllegalStateException.

Only a hunch but I have a feeling it is because I close the output stream and flush.

The image just doesn't display (obviously) any thoughts on how to handle this?



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JSP is merely a template for an HTML page. So how do you show an image in an HTML page? Do you just blat out the bytes into the HTML? Of course not.

Image bytes in a JSP are useless. You need to use an <img> tag, just as in any other HTML page.

The URL for that image, on the other hand, can reference anything on the server that returns image data. So write a serlvet that returns the image data (with appropriate headers set) that you reference from the <img> tag.
 
Carl Jenkins
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realized my mistake here as well.

Although, I did find this towards the bottom looks useful.

http://fdegrelle.over-blog.com/article-992927-6.html
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bad blog.

JSP response == text
image value != text

You do the math!

Use a servlet to serve the image.

Also, Java code in a JSP is so 2002. This is 2010...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!