0

I want to display image in my JSP retrieved out of Database. Now, after a lot of research on internet, I figured out this code.

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>View Item</title>
    </head>
    <body>
        <center>
            <h1>View Item</h1>
            <%
            Connection con = DefaultConnection.establishSqlConnection();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM Items WHERE ItemCode='"+request.getParameter("itemCode")+"'");
            Blob image = rs.getBlob("ItemImage");
            InputStream imageInputStream = image.getBinaryStream();
            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];
            int len=0;
            %>
            <form action="addItem.jsp" method="post">
                <table>
                    <tr><%=rs.getInt("ItemCode")%></tr>
                    <tr><%=rs.getString("ItemName")%></tr>
                    <tr><%while ((len=imageInputStream.read(buffer)) != -1) {
                        out.write(buffer, 0, len);
                    }%></tr>
                </table>
            </form>
        </center>
    </body>
</html>

Now my problem is that everywhere I saw, out.write() method is used with (byte[], int, int) arguments. But my NetBeans refuses to take those arguments. And API says that out.write() only supports (char[], int, int) arguments. I don't know if this way is right or not.

Please tell if I'm doing something wrong or if there is some other absolute method of doing this.

Keyur Golani
  • 573
  • 8
  • 26
  • 1
    You're currently attempting to corrupt the HTML output by inlining the entire binary content of images instead of printing HTML `` elements with the proper URL. This is not right. It's not the webserver who has to inline images in HTML document, it's the webbrowser who has to do that based on the `` elements in the HTML document. [Learn basic HTML](http://www.htmldog.com/guides/htmlbeginner/). Please also note that HTML `
    ` element is deprecated since HTML 4.01 in 1998 and please also note that using *scriptlets* in JSP is discouraged since JSP 2.0 in 2003.
    – BalusC Apr 29 '13 at 18:37
  • Yes, I know that. This is not a professional code. It is for educational purpose. I am a student. And I am supposed to do all these things. It is part of assignment. Plus, as you said, I should use '' tag, is it possible to direct access database with '' tag? Please elaborate on using '' tag to retrieve the data from Database. As I said, it is an assignment so I can not use Servlet in this one. – Keyur Golani Apr 29 '13 at 18:46
  • The blueish text in my 1st comment is clickable. – BalusC Apr 29 '13 at 19:00

0 Answers0