0

I would like to know how to display an image in a JSP page within a for loop. The image is accessed from a database. Below is my code and i'd like the image to be exactly where the line "<img src="<%= product.thumbnail %>"/>" is.

Short and simple chunk of my code:

<%
    for(int i=0; i<keys.length;i++){
        Product product = sdb.getProduct(keys[i].toString());
        out.println( "<p>" + product.title + "    " + "<img src="<%= product.thumbnail %>"/>" + "</p>" );
    }
%>

Thanks

--Added after edit--

Generated HTML:

<html>

<body>

<p>Linez    99.99    1</p>
<p>Stax    49.99    3</p>


<p> Order total = 249.96


<form action="order.jsp" method="post">
  <input type="text" name="name" size="20">
  <input type="submit" value="Place Order" />
</form>

<form action="basket.jsp" method="get">
  <input type="hidden" name="emptyBasket" value="yes">
  <input type="submit" value="Empty Basket" />
</form>

</body>
</html>
TheDerp
  • 131
  • 1
  • 10
  • Is the product.thumbnail a URL? When you say you fetch image from database, is it a base64 representation or URL? Can you also post the generated HTML for this JSP? Thanks. – Sid Mar 12 '16 at 05:04
  • @Sid just added the generated HTML and yes, that's the URL as the product holds the image in the database – TheDerp Mar 12 '16 at 05:12
  • I think the problem might be with the syntax. `<%` seem to be misplaced. Can you check that? Also, `` tags don't seem to be getting generated at all. Is there some exception? – Sid Mar 12 '16 at 05:17
  • There's no exception. I'm confused by this myself – TheDerp Mar 12 '16 at 05:24
  • Try having only one set of `<% %>` tags rather than nesting them. Finish a script tag before starting another. – Sid Mar 12 '16 at 05:27
  • I've fixed it. Thanks for your help anyway. I placed the data in a table (in the html) and placed the images within the table instead of calling it from within the java code in the jsp.     did the work – TheDerp Mar 12 '16 at 05:35

2 Answers2

0

I think you have a little more work to do to get this going. The img<> tag expects a URL, and cannot magically convert a database blob or similar to a URL.

My approach would be:

  • Create a servlet that serves thumbnail images from the database, and publishes URLs like /myApp/product/thumbnails?productId=12345
  • Test your servlet from a browser
  • Change your JSP to create these URLs.

Good luck.

kiwiron
  • 1,677
  • 11
  • 17
0

You need to use InputStream in your code i assure you this will work perfectly. for info search it on google it.

1) a.jsp page

<img src="image.jsp?imgid=<%=rs.getInt(1)%>" alt="<%= rs.getString("title")%>" style="width:280px; height:320px">

2) Put following code another jsp page

int id = Integer.parseInt(request.getParameter("imgid"));//imgid from a.jsp
try {

    String strQuery = "select book_image from tblbooks where id=" + id;
    ResultSet rs = st.executeQuery(strQuery);

    String imgLen = "";
    if (rs.next()) {
        imgLen = rs.getString(1);
    }
    rs = st.executeQuery(strQuery);
    if (rs.next()) {
        int len = imgLen.length();
        byte[] rb = new byte[len];
        InputStream readImg = rs.getBinaryStream(1);
        int index = readImg.read(rb, 0, len);
        st.close();
        response.reset();
        response.getOutputStream().write(rb, 0, len);
        response.getOutputStream().flush();
    }
} catch (Exception e) {
    e.printStackTrace();
}
Harshil Kulkarni
  • 411
  • 5
  • 20