Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

displaying images from a database

Status
Not open for further replies.

joelmac

Programmer
Jun 4, 2002
92
CA
Good Day,

I have a database of images (.jpg) stored in a MySQL database along with some information about each. I Know how to connect to the database and get strings, int's etc. How would i get one or more images from the database and display them on a web page using JSP?? Is it possible??

Thanks, JoelMac
 
You've got 2 options:

First option:
Don't save the images as OLE objects in the database, instead, save the hyperlinks to where each image is saved in your directory. By doing this, all you need to get from the database is the hyperlink (i.e., a string). You will then need to adjust these strings in your servlet so that the browser will be able to locate the image using the correct URL.

Second option:
Use the jpeg encoder that comes with J2SE but there is more work to this.
 
yeah i thought the first option would have to be the one, that will probibly work all right for me.

Would the second option need a Java applet on the page in order to work? i havn't used the jpeg encoder before.
JoelMac
 
No, jpeg encoder doesn't require an applet to view the image from but you could use an applet. To view it on a web page, you would need to set the content type as 'image/jpeg', meaning you couldn't display an other content type. I think the reason behing the jpeg encoder is to create on-the-fly graphics (using original images and personalising them with user input for example).

Hope this is helpful:)
 
Hmmm, that sounds pretty cool. There's got to be a way to get mixed content.

i supose inline frames could be used to seporate things, but i'd rather have everything on the same page, I think that would make it a little cleaner.

Thanks for the info finty. i'll keep up the research, any more information on the matter would be great JoelMac
 
Where would i find more information on the jpeg encoder any way? JoelMac
 
Would this work??

image.jsp:
Create a jsp page with the context type set to 'image/jpeg' that accepts an image id and gets the right image from the database and displays it.

display.html
Create another jsp or just an HTML page that has one or more img tags that points to the image.jsp passing in an image ID something like this <img src=&quot;image.jsp?imgID=xxx&quot;>

I don't realy have the know how to test this right now. What do you think? Is there any one with a lot of time on their hands that want to test this and send me the source?? HA HA! ________________________
JoelMac
 
Actually I am doing the same thing in my current project. Works fine and is definitely better then littering the file system with a ton of images and it also clusters well (compared to the file system approach).

I can't exactly send you the source though. Basically you will need to read the stream of bytes directly from the db and output them into your response. If you set the content type properly than it will work like a charm.

Of course most charms don't work...
 
cool sounds good, i can justify the time to figure it now that i know i'm on the right track. Thanks a bunch.

About the contect type is this the proper way to set it? response.setContectType(&quot;impage/jpeg&quot;); ________________________
JoelMac
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top