Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Annahita Moshiri
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic