-3

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>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</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>&nbsp;</td>
                <td><img src="<%= response.getOutputStream().write(bytearray,0,size)%>"width=50 height=50 /></td>
                <td>&nbsp;</td>
            </tr>
            <%
            }
            }     
            %>
            <tr>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </table>
    </body>
</html>
Community
  • 1
  • 1

1 Answers1

1

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

    <%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);

    /* you need an identifier for each image, like an ID */
    ResultSet rs=st.executeQuery("select name, image from img");

    %>
    <table width="100%" border="2">
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <%
        while(rs.next()) {
        %>
        <tr>
            <td>&nbsp;</td>
            <td><img src="${path_to_your_image_serving_resource}/<%=rs.getString("name")%>"width=50 height=50 /></td>
            <td>&nbsp;</td>
        </tr>
        <%
        }     
        %>
    </table>

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:

  • You do not use a connection pool, but instead the DriverManager and create a new connection to the database per request. This will neither perform well nor is it scalable. Look at javax.sql.DataSource
  • defining your SQL directly inside the JSP is (in general) a bad practice
home
  • 12,468
  • 5
  • 46
  • 54