Possible Duplicate:
How to retrieve and display images from a database in a JSP page?
I want to display an image in a <td> tag of a table. The code is working fine but I am not able to retrieve the image in the <td> tag of the table
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*,java.io.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<%Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:IMG");
Statement st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs=st.executeQuery("select image from img");
%>
<table width="100%" border="2">
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<%
while(rs.next()) {
byte[] bytearray = new byte[1048576];
int size=0;
InputStream sImage = rs.getBinaryStream(1);
response.reset();
response.setContentType("image/jpeg");
while((size=sImage.read(bytearray))!= -1 ){
%>
<tr>
<td> </td>
<td><img src="<%= response.getOutputStream().write(bytearray,0,size)%>"width=50 height=50 /></td>
<td> </td>
</tr>
<%
}
}
%>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</body>
</html>
Setting a different content type inside an HTML (jsp) page cannot work. Inside your JSP you must generate a link to an image. The browser will request the image in a separate request, for this you need something like a servlet:
1. step: generate the JSP
2. step: create a servlet
Your servlet must listen to
${path_to_your_image_serving_resource}/*. If the browser requests this URL you have to retrieve the image based on its name, set the coorect content type for the response, and stream the bytes back to the browser.Remember that in HTML/HTTP an image is an independent resource.
BTW: You should carefully look at your design:
javax.sql.DataSource