Displaying an image in JSP

A

Amardeep Verma

Hi,
I have an image, in JPEG format, stored inside a byte array.
I have to render that image. I will not be able to open a file
connection and write that image to HDD, either on client side or on
server side. So my query is this, how do I render an image which is
stored as a series of bytes? I have tried out.println and IMG tag, but
they do not work.

Any help would be appreciated

Bye
Amardeep Verma
 
A

Andy Fish

presumably your JSP page is currently producing HTML. HTML cannot contain
image data in the file itself, only through an external href.

so the best option would be to generate an <img> tag in your JSP which
contains a URL to a different JSP (or the same jsp with different
parameters). When the browser requests the image, you can send the binary
data as content type image/jpeg.

Andy
 
C

Chris

Amardeep Verma said:
Hi,
I have an image, in JPEG format, stored inside a byte array.
I have to render that image. I will not be able to open a file
connection and write that image to HDD, either on client side or on
server side. So my query is this, how do I render an image which is
stored as a series of bytes? I have tried out.println and IMG tag, but
they do not work.

I haven't tried this, but my guess is that you could include a tag like this
in your html: <img src="myimage.jsp">. Then have myimage.jsp return the byte
data. Be sure to set the http response header to indicate that it's a jpeg.
 
R

Raymond DeCampo

Chris said:
I haven't tried this, but my guess is that you could include a tag like this
in your html: <img src="myimage.jsp">. Then have myimage.jsp return the byte
data. Be sure to set the http response header to indicate that it's a jpeg.
FYI, since JSPs implicitly open the output stream as a character stream
and the specification prohibits one from re-opening it as a byte stream,
this is best done from a servlet instead of a JSP.

Ray
 
R

Raymond DeCampo

Allen said:
I think you can set the MIME type you are returning in both servlets and
JSP. SO, you could set the MIME type being returned to image\jpeg or
whatever, convert the byte[] to a character[], and then just slam it
through.

No; you can set the MIME type if you like, but a JSP is only allowed to
return character data. There was a tech tip from Sun a while back that
dealt with this very issue. The original tip suggested using JSPs, but
the next tip made the correction. In fact, I just found the tip in my
email library; it wasd the tech tip for 8/21/2001.

Here are the links:
http://developer.java.sun.com/developer/JDCTechTips/2001/tt0821.html
http://developer.java.sun.com/developer/JDCTechTips/2001/tt0821_update.html

Ray
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top