Click to See Complete Forum and Search --> : Retrieving image from database


edhinaaid
03-21-2007, 12:17 AM
Hi Folks,
By Using this code I can retrieve image from database. but I want to insert this image in Jsp page. How can we do that?

PreparedStatement pstmt = conn.prepareStatement("select photo from member_data where userid= ?");
pstmt.setString(1, userID);
ResultSet rs=pstmt.executeQuery();
if(rs.next()) {
imageBytes = rs.getBytes(1);
photo = Toolkit.getDefaultToolkit().createImage(imageBytes);

return photo;

potterd64
03-22-2007, 01:28 PM
You could put the code in the JSP itself, minus the return statement and the photo creation.
Then try this.

<%@page import="...,javax.servlet.ServletOutputStream"%>
...
response.reset();
response.setContentType("image/jpeg");
response.addHeader("Content-Disposition","filename=image.jpeg");

ServletOutputStream ostr = null;

ostr=response.getOutputStream();
ostr.write(imageBytes,0,imageBytes.length);
response.flushBuffer();


then you can use the jsp as a src in another page.
<img src="getImage.jsp">