File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Displaying image from database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Displaying image from database" Watch "Displaying image from database" New topic

Displaying image from database

Aravind Prasad
Ranch Hand

Joined: Dec 28, 2005
Posts: 265
Dear Ranchers,

I am doing a project in JSP and MySQL. I have the image inserted in the database. I have to retrieve the image and display it in the jsp page. I am using a framework in such a way, the class files will connect to the database and retrieve the table values and save it in the hash map.

it is retrieving the image and store in the HashMap like this

How can I put the emp_photo in the jsp page.

Please assist me.

Thanks and Regards

Aravind Prasad

[ UD: fixed code layout ]
[ November 17, 2007: Message edited by: Ulf Dittmer ]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
What kind of object is that that you have stored in the database? A byte array[] of an image? Is it in a web-compatible format (GIF, JPEG, PNG)?

Either way, you can't do this in a JSP - you'll need a servlet for it. JSPs can handle text, but not binary data. The JSP page that includes the image should have an image tag that has a "src" attribute pointing to the servlet that retrieves the image. The servlet would then stream the image data to the browser, setting the content type according to the image format. The CodeBarn contains an example of how to do that.
Aravind Prasad
Ranch Hand

Joined: Dec 28, 2005
Posts: 265
Thankyou very much Ulf

It can work well now!!


Aravind Prasad
I agree. Here's the link:
subject: Displaying image from database
It's not a secret anymore!