2

I have a web app (JSF 2.0) that should display some images.

I read in this thread and implemented an image servlet to send images to the client. Problem is, I am not using a database nor fixed location on the hard drive, just a backing java method returning a byte[] (For the discussion, let's say it's generated on the fly)

I am wondering if there is a 'standard' solution for this problem, namely, How do you display an image that is generated on the fly on a webpage?

UPDATE I eventually decided to use RichFaces MediaOutput Component. The answer that is marked as correct is, in my opinion, more robust and better for most cases.

Community
  • 1
  • 1
Ben
  • 10,020
  • 21
  • 94
  • 157
  • Whether the bytes of the image come from a database, a file, the memory or anywhere else doesn't matter. The browser just needs the bytes and the content type. What problem do you face exactly? – JB Nizet May 24 '11 at 12:56
  • @JB getting the `` to display the image from a `byte[]` returned by a backing bean method. – Ben May 24 '11 at 12:57
  • You could generate a unique ID (a counter would be sufficient) for the generated image, store the image in a map, indexed by this key, and put this id as a parameter of the URL to the image servlet used in h:graphicImage. The servlet would then get the image from the map, using the key, and serve the image. – JB Nizet May 24 '11 at 13:03

1 Answers1

1

Move the byte[] code generating logic from the backing bean to the image servlet. All basic parameters which the backing bean uses to generate the byte[] should be passed as request parameters to the image servlet. E.g.

<img src="imageservlet?param1=#{bean.param1}&param2=#{bean.param2}" />

If that is really not an option for some reason, best what you could do is to let the backing bean store those images temporarily on temp disk or session and then pass its identifier as request parameter so that the servlet can locate it and write it to the response body.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Tnx. I wish I had thought about that :-) (The image generating logic replication) – Ben May 24 '11 at 12:59
  • Eventually I decided to use `richfaces` `mediaOutput` component since I found it too complicated to regenerate the image. – Ben May 24 '11 at 13:45
  • Ah yes, that's also an option. Didn't thought about it. It basically stores the byte stream temporarily in the session and uses a resource resolver to stream it back. I believe PrimeFaces [``](http://www.primefaces.org/showcase/ui/media.jsf) does effectively the same. – BalusC May 24 '11 at 13:51