Ho to display image getting from byte array in JSP ?
Joined: Jul 30, 2009
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 ?
Joined: Mar 22, 2005
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).
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.