Web tier: servlets, JSP, Web frameworks: help,how to display a photo which have got from DB BLOB in jsp
- Posted by: wu shuangyue
- Posted on: September 01 2005 20:58 EDT
how to display a photo which have been got from database BLOB field in jsp?
tanks and regards
- display a photo which have got from DB BLOB in jsp by Ashish Jamthe on September 02 2005 01:15 EDT
- if you want best approach by Anand kishore on September 02 2005 01:57 EDT
- display a photo which have got from DB BLOB in jsp by Emil Kirschner on September 03 2005 09:36 EDT
Do the following.
1. Use the html:image tag
Where n is the id for the image in the DB
2. Write DisplayImageServlet on the lines of
byte image = persistence.loadImage(n);
3. Now about converting BLOB into byte array.
You could do this using the getBytes method in java.sql.Blob interface. However, this may not work for Oracle 9i as getBytes returns the Oracle blob locator. For this case there is an excellent blog which worked for me.
Generate the dynamic URL for your image to hit the same server and send the response. When it is trying to display the image, once again a new request will hit your server. At that time just stream the image by using response PrintWriter.
This code looks nice, but you may want to avoid loading the entire image in memory ( byte image = persistence.loadImage(n); ). If you have many simultaneous connections this will end up allocating a lot of unnecessary (potentially big) memory and your webapp's memory footprint will increase in an unpredictable way, which will end up giving more work to the garbage collector and reduce the number of active sessions you can keep in memory.
Instead of loading the entire image each time, you may want to consider a streaming approach. The JDBC api lets you stream data from blobs. You can you use this feature to transfer your binary content in small chunks, ideally using statically allocated buffers.
Emil Kirschner ( http://www.thekirschners.com/ )