2

Ok so i am trying to display an image along with user first name and last name on a jsp page but i only get the image not the first name and last name...so what's the problem here? ...your help will be appreciated:)

<%@ page language="java" contentType="text/html; charset=windows-1256"
        pageEncoding="windows-1256"%>
        <%@ page import="java.sql.*" %>
         <%@ page import="java.io.*" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
    <title>Insert title here</title>
    </head>
    <body>
     <H1>Fetching Data From a Database</H1>
    <%

    Blob image = null;  
    byte[] imgData = null;  
    Connection con;
    String url="jdbc:mysql://localhost:3306/image";
    String uName="root";
    String pwd="root";
    Class.forName("com.mysql.jdbc.Driver").newInstance();
        con=DriverManager.getConnection(url,uName,pwd);
        String sql="Select * from image ";
        PreparedStatement stmt=con.prepareStatement(sql);

        ResultSet resultset=stmt.executeQuery();
    while(resultset.next())
    {

        Blob bl = resultset.getBlob("image");
        byte[] pict = bl.getBytes(1,(int)bl.length());
        response.setContentType("image/jpg");
        OutputStream o = response.getOutputStream();



    %>
    <TABLE BORDER="1">
    <TR>

    <TH>First Name</TH>
    <TH>Last Name</TH>
    <TH>picture</TH>
    </TR>
    <TR>

    <td>Image</td><td><%o.write(pict);%></td>
        <%o.flush();
        o.close();%>

    <TD> <%= resultset.getString(2) %> </TD>
    <TD><%= resultset.getString(3) %></TD>
    </TR>
    </TABLE>
    <BR>
    <%
    o.flush();
    o.close();
    }
    %>
    </body>
    </html>
user2137186
  • 817
  • 6
  • 22
  • 39
  • @TheEwook i have tried that method but cant access even making servlet ...:( – user2137186 Apr 07 '13 at 11:17
  • 1
    @user2137186 - you cannot dump the image content directly into the page like that. What problems are you having creating servlets? Perhaps you should try again, then post a SO question if you run into trouble (after appropriate research effort, of course). – Perception Apr 07 '13 at 11:24
  • ok i pass id like that and then get data in input stream what to do after that? – user2137186 Apr 07 '13 at 11:35

1 Answers1

0

Your code includes calls to o.close() after writing the picture out and later on. That will close the response output stream, and nothing more will be sent out. Don't do that - let the web container take care of closing the output stream.

That should explain why the first name and last name shouldn't be displayed: the response stream was closed.

Also, I don't think the response content type should be "image/jpg" especially considering how the HTML meta tag says the content is "text/html". I think they should be the same - "text/html".

Finally - and maybe most importantly? - JSP is very old. You may want to consider using Facelets (JavaServer Faces technology).

Ahmed
  • 590
  • 8
  • 19
  • 2
    The fact that something is old does not necessarily make it outdated. Besides, even if it is outdated, suggesting that the OP should switch technologies is unlikely to help him finish his current task / project. – Stephen C Apr 07 '13 at 11:20
  • My answer included a suggestion, with the expression "may want to consider". There is nothing condescending or negative about this, and in fact you will find the same sentiment expressed in multiple replies to JSP questions on this very site. – Ahmed Apr 07 '13 at 11:25
  • @Ahmed have you ever displayed a blob image on jsp page? – user2137186 Apr 07 '13 at 11:36
  • Dear @user2137186, you can find several already written answers talking about that on this site, including the one that has been linked to this very question. ... However, I tried to help you with the "failure to display firstName and lastName" problem - it was the response stream being closed. – Ahmed Apr 07 '13 at 11:41