TheDerp TheDerp -4 years ago 88
Java Question

Display image (from database) in JSP

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 %>"/>"

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>" );


--Added after edit--

Generated HTML:



<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 action="basket.jsp" method="get">
<input type="hidden" name="emptyBasket" value="yes">
<input type="submit" value="Empty Basket" />


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download