Marvin Ian Chua
Marvin Ian Chua

Reputation: 1

Why is my image displaying a default icon in the jsp ? there is an image below

*Here is the JSP *

<% try {
                String connectionURL = "jdbc:mysql://localhost:3306/mydb";
                Connection connection = null;
                Statement statement = null;
                ResultSet rs = null;
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                connection = DriverManager.getConnection(connectionURL, "root", "alienware");
                statement = connection.createStatement();
                String QueryString = "SELECT Warehouse_Stock.name ,Warehouse_Stock.photo from Warehouse_Stock";
                rs = statement.executeQuery(QueryString);




        %> 
        <table class = "hovered" id="info" cellpadding="15" border="2">
            <thead>
            <tr>
                 <td>Photo</td>
                <td>Product Name</td>
                <!--<td>Contact Number</td>
                <td>Remarks</td>
                <td>Email Address</td>-->

            </tr>
                </thead>
            <%
                while (rs.next()) {
            %>
            <TR>
                 <td><img src="getImageDetails.jsp?your_id=12"  /></td>
                <td><%=rs.getString(1)%></td> 

                 <%--<td><%=rs.getBlob(1)%></td>--%>
                <%-- <td><%=rs.getInt(2)%></td>
                 <td><%=rs.getString(3)%></td>
                 <td><%=rs.getString(4)%></td>--%>


            </TR>

Here is the servlet

    response.setContentType("image/jpeg");
    PrintWriter out = response.getWriter();

    int img_id = Integer.parseInt(request.getParameter("product_code"));
    DBConnectionImp db = new DBConnectionImp();
    Connection con = db.getConnection();
            ResultSet rs = null;
    PreparedStatement pstmt = null;
    OutputStream oImage;
    try {
        pstmt = con.prepareStatement("SELECT Warehouse_Stock.photo from Warehouse_Stock");
        pstmt.setInt(1, img_id);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            //byte barray[] = rs.getBytes(1);
            //byte barray[] = rs.getBytes(1);
            //response.setContentType("image/jpeg");
            ////oImage = response.getOutputStream();
            //oImage.write(barray);
           // oImage.flush();
            //oImage.close();

            Blob blob = rs.getBlob(1);

            //response.setContentType("image/jpeg");
            oImage = response.getOutputStream();
            oImage.write(blob.getBytes(1, (int) blob.length()));
            oImage.flush();
            oImage.close();



        }
    } catch (Exception ex) {
        //ex.printStackTrace();
    } finally {
        try {
            if (con != null) {
                con.close();
            }
        } catch (Exception ex) {
            // ex.printStackTrace();
        }
    }

You see there are images showing but these images are all the same. These images are not really images but icons that represents a broken image like when you load a browser that doesn't support flash . The ones you see when your browser doesn't support flash is the one I am talking about. But it is not an "f" but a icon that represents a broken image.

Upvotes: 0

Views: 286

Answers (1)

JB Nizet
JB Nizet

Reputation: 691953

All the images are loaded from the URL

getImageDetails.jsp?your_id=12

So this URL doesn't point to a servlet, but to a JSP. And even if it was pointing at your servlet, the servlet expects to find the ID of the image to load in the parameter product_code, but you're passing the parameter your_id.

Upvotes: 1

Related Questions