Link to home
Start Free TrialLog in
Avatar of AntoniRyszard656
AntoniRyszard656

asked on

Displaying images from Database?

Hello,

In my web application I plan to store thumbnail images and the orginal images on a database.

The thumbnail images will be displayed to the user in a jsp page. Could anyone advise, if the thumbnails are stored as type blob on the database. How in the servlet would I change this blob back to a jpg, gif image?

And could anyone advise having displayed the thumbnail images in jsp. When the user clicks on the thumbnail I would like to display the full-sized image, to achieve this would I need to contact the display again to get the full sized image?

Thank you  
ASKER CERTIFIED SOLUTION
Avatar of fargo
fargo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AntoniRyszard656
AntoniRyszard656

ASKER

Thanks

Could I finally ask having displayed the thumbnail images in the jsp. When the user clicks on the thumbnail I would like to display the full-sized image, to achieve this would I need access the database each time the user clicks the thumbnail to retrieve the full-sized image?



if you are displaying only one image in a page, i would better retrieve both thumbnail and bigger image at the same time from the database. Then i would use css to show the big image only one user clicks the thumbnail. If there are multiple images in the page, use the db retrieval for each image seperately on the click.

There are many examples you will find googling about the thumbnail to big using css. Here is one link for such purpose
http://www.webmasterworld.com/forum21/10007.htm

Hope it helps.
Thanks

The jsp could have upto 40 thumbnails, so I guess the larger images will need to come from the database separately.

My other thought was to store the images on the server and create an account folder for each user. Then make a reference to this file on the database.

Do you believe storing files on the server is often best approach, or can the file handling become very difficult?
it is a trade-off..if the traffic of your application is too high and you think that retrieving/uploading the images directly into the server can become a problem..better keep a reference. But keeping a reference may have it's own side effects like maintenance of these images on the file system seperately.

Overall, it depends. Personally in some of apps i worked with..we keep images on the db blob and do the retrieval.
Check out the performance in your case with 40 images and if you see it as a bottleneck, do the file system handling then.
Thanks for your advise.

When using the GIFEncoder class or ImageIO, would this actually produce an file which we use could call in the jsp using the html tag src?

Thanks
actually with the given code, we are writing the image directly to the output stream of the servlet. So in the src tag, you just give the path to the servlet.
In my application I was trying to use the MVC model.

In the other servlets I pass a javabean to the jsp.

Would I need to take a direct approach to your above code if I use javabean.
Sorry I meant

Would I need to take a different approach to your above code if I use javabean.
yes in my approach, direct access to servlet is given. You may acheive the same with javabean but the handling will be different.

P.S: off for dinner. Hope you get the clear idea.
Thanks

I wondered if you possibly have a moment tomorrow to post, I would be very interest to see how to use javabeans to pass the images from the servlet to the jsp.

Or an example on the web.

Thanks again
 
for using it with bean, what u can do is to set the byte[] property for the bean and use some custom jsp tag (u have to create one on your own) to use the Encoder and write it to JspWriter instead of servlet output stream.

Or the other approach is what we already discussed with servlet responsible for creating the images.

Thanks

I think it would be better for the servlet to be responsible for creating the images.

The only I could not understand is how to pass the thumbnail images from the servlet to a jsp page. Would I need to use the HttpServletResponse?

Could you recommend a web-site example?

Thank you
The only I could not understand is how to pass the thumbnail images from the servlet to a jsp page. Would I need to use the HttpServletResponse?
>> but this is what we discussed isn't it. That u retrieve the thumbnail images (blob) from db and render it from servlet using the output stream. You have to set the content type in the servlet.
Thanks

Would I be correct in saying one example could be to have a html page which asks the user to choose a category.

This category is posted to a servlet, and the servlet accesses a database and retrieves the filenames. Then the servlet uses a javabean to pass these filenames to a jsp page.

In the jsp we have a src or href for each of the javabean filenames, and these src/href automatically post the filename to another servlet which returns the blob image in a stream based on your code.  

Does this sound the correct approach.
So the servlet which returns the blob as a stream would be called separatly for each of the thumbnail images.
This could be the source of your jsp to display the images, where the filename is coming from bean.(iterate if u have many filenames)

<img src='YourServletToRenderImage?filename=<bean:write name="filename" />' border="0" height="20" width="40">

You can handle the image display based on filename handling in the servlet.(using the same servlet)

In the servlet you first define the content type and other header params to avoid caching etc like below
response.setContentType("image/x-png"); // should use jpeg for jpeg rendering
response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
response.setHeader("Pragma", "no-cache"); //HTTP 1.0
response.setDateHeader("Expires", 0); //prevents caching at the proxy server
.....other handling with db etc
....write image to output stream

that's all.

Hope it gives u a clear idea.