• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Ho to display image getting from byte array in JSP ?

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Thomas Houseman
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok i will try that.
thanks !
 
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic