• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

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  
0
AntoniRyszard656
Asked:
AntoniRyszard656
  • 9
  • 8
1 Solution
 
fargoCommented:
how to handle it from db resultset

            while (rs.next()) {
                try {
                    // Get as a BLOB
                    Blob aBlob = rs.getBlob(1);
                    image = aBlob.getBytes(1, (int) aBlob.length());
                } catch (Exception ex) {
                    try {
                        // The driver could not handle this as a BLOB...
                        // Fallback to default (and slower) byte[] handling
                        image = rs.getBytes(1);
                    } catch (NullPointerException npe) {
                        logger.error("NullPointerException :- Image not found");
                    }
                }

In the servlet do the following...

            // preparing output stream
            ServletOutputStream os = response.getOutputStream();
            java.io.InputStream is = null;

            is = new ByteArrayInputStream(GET_IMAGE_BYTEARRAY); // as i said in the start of the post
            GIFEncoder gifEncoder = new GIFEncoder(Jimi.getImage(is));
            gifEncoder.Write(os);

            os.flush();
            os.close();

i m using GIFEncode from acme. Sun don't have writing gifs...have a look at the following link and see the GIFEncoder class....or use ImageIO

http://mindprod.com/jgloss/gifencoder.html
0
 
AntoniRyszard656Author Commented:
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?



0
 
fargoCommented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
AntoniRyszard656Author Commented:
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?
0
 
fargoCommented:
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.
0
 
AntoniRyszard656Author Commented:
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
0
 
fargoCommented:
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.
0
 
AntoniRyszard656Author Commented:
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.
0
 
AntoniRyszard656Author Commented:
Sorry I meant

Would I need to take a different approach to your above code if I use javabean.
0
 
fargoCommented:
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.
0
 
AntoniRyszard656Author Commented:
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
 
0
 
fargoCommented:
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.

0
 
AntoniRyszard656Author Commented:
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
0
 
fargoCommented:
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.
0
 
AntoniRyszard656Author Commented:
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.
0
 
AntoniRyszard656Author Commented:
So the servlet which returns the blob as a stream would be called separatly for each of the thumbnail images.
0
 
fargoCommented:
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.

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now