0

i am trying to display table data from database to jsp page, all data are fetched but image is not loaded into image tab. i have store image as a BLOB in table.

my jsp page is follow :

<body>
    <table border="1">  

    <%@ page import="com.*;"%>
    <%
        MySql myobj = new MySql();
        myobj.connect();

        String query="select contact_id,first_name,last_name,photo from contacts";

        ResultSet rs = myobj.getdata(query);

        while(rs.next())
        {               

        %>
            <tr>
                <td>
                    <%= rs.getInt(1) %>             
                </td>

                <td>
                    <%= rs.getString(2).toString() %>
                </td>
                <td>
                    <%= rs.getString(3).toString() %>
                </td>

                <td>
                    <img src="<%= rs.getString(4) %>"  width="500" height="300" alt="data"></img>                   
                </td>

            </tr>
        <% 

        }
    %>


    </table>
</body>

i have to also implement download when item name or data of second div is clicked then that related data should be downloaded.

can anyone solve my this problem soon ?

how to implement download in servlets as back end ?

i have write this code for imageservlet :

protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {   
        String id = request.getParameter("contact_id");     
        MySql myobj = new MySql();
        myobj.connect();
        PrintWriter out=response.getWriter();       
        try
        {
            String query="select photo from contacts where contact_id='"+id+"'";

            ResultSet rs = myobj.getdata(query);

            out.println(id);

            Blob image=null;
            byte[] imgData = null;
            image= rs.getBlob(1);
            imgData = image.getBytes(1, (int) image.length());
            response.setContentType("image/gif");
            OutputStream o = response.getOutputStream();
            o.write(imgData);
            o.flush();
            o.close(); 
            response.sendRedirect("Message.jsp");
        }
        catch (Exception e)
        {
            e.getMessage();
        }       
    }
  • the attribute `src` of the tag [img](http://www.w3schools.com/tags/tag_img.asp) specifies the [URL](http://de.wikipedia.org/wiki/Uniform_Resource_Locator) of an image. what you are saving in the DB is the image data as blob. So what you have in your JSP for displaying the image cannot work. – A4L Apr 19 '13 at 10:32
  • so for that what i have to do sir ??? –  Apr 19 '13 at 10:39
  • You can find solution from this: [Display blob image through jsp](http://stackoverflow.com/questions/11192020/display-blob-image-through-jsp) – Navand Apr 19 '13 at 10:45
  • [Here](http://stackoverflow.com/questions/2429934/is-it-possible-to-put-binary-image-data-into-html-markup-and-then-get-the-image) is another how you achieve it. And Btw do not use the resultset directly from your jsp, try to put the Data you are trying to display in a separate class object – A4L Apr 19 '13 at 11:16

1 Answers1

1

You should use a servlet to serve up the image with appropriate content-type header

<img src="img-servlet?contact_id=<%= rs.getInt(1) %>"  width="500" height="300" alt="data">

And, in your servlet get the contact_id query param and fetch the image contents

shyam
  • 9,134
  • 4
  • 29
  • 44