Skip to main content

Images, databases and jsf - how to make it work?

4 replies [Last post]
janpla
Offline
Joined: 2010-08-25
Points: 0

I am trying to get my head around how to do this - I hope somebody can tell me how, please.

I have a database table in which I want to store image files (like .png, jpg, etc) - something like:

table images(
  key int,
  blob bigpicture
)

I want to extract pictures from the table and display them in a jsf page; I know how to go about database tables etc in general in Java EE, but I am not sure about images - is there a place I can read more about it? Or is it just really simple?

Another things is - how about uploading pictures, which is the other, important part of this?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ImDeadsoul
Offline
Joined: 2012-04-19
Points: 0

I wouldn't say that it is really hard, but there is some stuff you have to do. If you have set the entity and dao to insert images to db then its cool. The only thing you have to do is create a servlet which will catch the request params and search the image in the db and show it using stream.

You can find a good example here:

JSF - Displaying images from database in JSF

oversteer
Offline
Joined: 2011-03-28
Points: 0

Really simple it isn't I'm afraid. You can start reading this to get a handle on getting the data out of the
database: http://balusc.blogspot.com/2007/04/imageservlet.html. Getting the data into the database
in the first place is probably an even bigger nightmare.
You're going to need a component library like Richfaces and use their rich:fileUpload. I have to say that
I've never had any success using it myself, I think because I use container security. The myfaces tomahawk
library has t:inputFileUpload which I have got working, although it has many limitations, doesn't support
ajax and has to be in a multipart/form-data form. BalusC also has a file upload offering but I couldn't get
that working either at the time I tried it, but it might work for you (handy if you don't want the baggage
of a full component library for just one component).
I'd strongly recommend allowing a lot of time to get this working. On a postitive note Mojarra 2.2 is, I
think, going to contain a file upload component which is very good news because nobody else seems to
be much good producing one that works easily for everyone.
The various component library file upload offerings (i.e. richfaces, primefaces etc...) typically either require
you to configure a filter in the web.xml or use an @WebFilter annotation in your code. Especially if you are
evaluating file upload components from different providers, it's easy to end up with conflicting filter
configuations, so keep an eye on this. It's particularly easy to forget to comment out an @WebFilter
annotation that is no longer needed.

janpla
Offline
Joined: 2010-08-25
Points: 0

Thanks a lot!

Doesn anybody know when Mojarra 2.2 is due out?

Stoney
Offline
Joined: 2006-02-17
Points: 0

Uploading an image into the database is not such a big deal. However, extracting it and displaying it in the database seems to be more of a problem. I managed to extract an image from the database using the posting at http://balusc.blogspot.de/2007/04/imageservlet.html.

However, that only works when I manually enter the Image URL in the address line of the browser, e.g. http://localhost:8080/myapp/image?id=aeb3e5... Here, the servlet is being called and the image is displayed stand-alone in the browser.

If, however, I try to include the image in a JSF site (e.g. ) it does not work for me. The backing bean does extract the hash value (aeb3e5...) which is then rendered e.g. in . However, I would like to have the servlet being called and thus make JSF to return the JSF site along with the image.

What could the problem be? Is my understanding not correct in the sense that the server can render a JSF site and include the binary stream in it?