[b]Here is the JSP page that calls the servlet to show the images:[/b]
<%@ page contentType="text/html;charset=windows-1252"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>untitled</title>
</head>
<body valign=center>
<table>
<tr>
<td>
<img src="/test-testimgs-context-root/servlet/test1.images.servlet.GetImage">
</td>
<tr>
</table>
</body>
</html>
[b]Here is the code that resides in the service method of GetImage servlet:[/b]
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
OutputStream stream = response.getOutputStream();
Blob imgBlob = null;
ResultSet rs = null;
byte[] imbBytesAry = null;
try
{
ImageTest imgtest = new ImageTest();
rs = imgtest.get("img1.tif");
if(rs != null)
{
while(rs.next())
{
imgBlob = rs.getBlob(1);
int len = new Integer( new Long( imgBlob.length()).toString() ).intValue();
imbBytesAry = imgBlob.getBytes(1,len);
if (imbBytesAry.length == 0 || imbBytesAry == null)
{
noImageFound(request, response);
}
else
{
stream.write(imbBytesAry);
}
}
}
else
{
noImageFound(request, response);
}
}
catch (Exception e)
{
noImageFound(request, response);
}
}
[b]Here is the code that resides in the get method of ImageTest class:[/b]
public ResultSet get(String fileName) throws DatabaseConnectionException, SQLException, Exception
{
Connection connection=null;
ResultSet rs = null;
ResultSet rs1 = null;
Blob imgBlob = null;
try
{
connection = new ConnectionProvider().getCConnection() ;
connection.setAutoCommit(false);
Statement stmt = connection.createStatement();
String sql = "SELECT IMG FROM IMAGE_TABLE WHERE IMAGE_NAME = ? ";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1,fileName);
rs = pstmt.executeQuery();
boolean flag = rs.next();
if(flag)
{
rs.beforeFirst();
}
else
{
sql = "SELECT IMG FROM IMAGE_TABLE";
rs1 = stmt.executeQuery(sql);
boolean flag_all = rs1.next();
if(flag_all)
{
rs1.beforeFirst();
}
else
rs1 = null;
}
return rs;
}