File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Ho to display image getting from byte array in JSP ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Ho to display image getting from byte array in JSP ?" Watch "Ho to display image getting from byte array in JSP ?" New topic
Author

Ho to display image getting from byte array in JSP ?

Thomas Houseman
Ranch Hand

Joined: Jul 30, 2009
Posts: 31
Hi,
I receive an image as byte array (byte[]) from a servlet to a jsp page and i'm trying
to display this image in the jsp.
How should I do that ? How should I transfer this byte[] to image which I can see in the jsp page ?

Thanks !
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
Handling binary data should be left to the servlet; JSPs are meant for textual output (like HTML).

The two ways I see are a) store the byte[] as a file with a known name, and then use that filename in the the img tag of a JSP page, or b) keep the data in memory (maybe as a session or application attribute), and have a servlet stream it to the client (with the servlet's URL in the image tag's "src" attribute).


Ping & DNS - my free Android networking tools app
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

As Ulf has pointed out, you've put the cart before the horse.

Remember that all a JSP is is a way to create an HTML page to send the browser. And when putting an image into an HTML page do you embed binary data? Of course not. You create an image tag that references a resource (usually an image file) that serves the binary data.

Same thing here. You shouldn't be fetching the image data at all in the request that generates the JSP. Rather, create an image tag on the JSP that references a different servlet that serves up the binary data when asked for it.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Thomas Houseman
Ranch Hand

Joined: Jul 30, 2009
Posts: 31
ok i will try that.
thanks !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ho to display image getting from byte array in JSP ?