• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to show byte[] as image in jsp page?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello mates!

I have successfully stored some images in my db, and now I want to show them in my jsp page using struts.
Here's what I have done:

First I read the images from db and store them in an ArrayList, called allPictures:


Then in my Action, all I do is:


now that I have my arraylist of byte[]s , I want to show them in my page (which contains other information about my speciman, besides it's pictures):



I don't know what should I put instead of "?". How can I use <html:img> tag here? :roll:
Thanks in advance!
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Annahita,
Your Struts action needs to write these images to somewhere on the server. Then the loop can point to that location.

Browsers require images to be separate files. As a result, you can't just write out the images inline.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another possibility would be to create an action that gets a single image from the database by specimen Number and writes it to the outputStream of the HttpServletResponse object with the header set to the proper MIME type. Or even easier: extend the org.apache.struts.actions.DownloadAction class provided by Struts.

Then within the loop in your JSP, use the html:img tag to call the action and display the image.

Example:
 
Annahita Moshiri
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your replies!

Merril:
When I try to add the action part to <html:img> tag:

I get this error in compile:

I use struts 2 by the way. How can I fix this?! (as you can see I'm a greenhorn!)

Jeanne:
If I want to choose this approach, how can I take care of deleting those images from the file system afterward? I think after some queries there will be many images left uncared in the file system. How can I deal with them? If I can trust the users to always click "logout", I think I can take care of deleting those temporary images, but what if they just close their browser without logging out?
 
Jeanne Boyarsky
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Annahita,
I think Merril meant 'src".

As for the cleanup, you can create a session listener to detect when a session invalidates itself. this will trigger on explicit logout and session timeout.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you were using Struts 2, you would not be using a <logic:iterate> tag. You must be using some version of Struts 1. The latest version of Struts 1 (1.3.10) does allow an action attribute for the html:img tag. However, if you're using a version that doesn't allow it, just use page="myImageAction.do".
 
reply
    Bookmark Topic Watch Topic
  • New Topic