aspose file tools*
The moose likes Struts and the fly likes How to show byte[] as image in jsp page? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to show byte[] as image in jsp page?" Watch "How to show byte[] as image in jsp page?" New topic
Author

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

Annahita Moshiri
Greenhorn

Joined: May 22, 2008
Posts: 9
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30516
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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:


Merrill
Consultant, Sima Solutions
Annahita Moshiri
Greenhorn

Joined: May 22, 2008
Posts: 9
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30516
    
150

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

Joined: Feb 15, 2005
Posts: 4864
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".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to show byte[] as image in jsp page?