File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Display images/ in WEB-INF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Display images/ in WEB-INF" Watch "Display images/ in WEB-INF" New topic

Display images/ in WEB-INF

Raj Maheshwari
Ranch Hand

Joined: Jul 02, 2004
Posts: 34
I have a JSF application in which I have some images that can be only viewed by users logged into the system.

I have a Login Filter created and tested. It works file for restricted pages.

My question is:

1) How can I display pictures in a restricted fashion (must pass the login filter)
2) If the images are in the WEB-INF folder what value/url do I give to the h:graphicImage component?

Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
If the images are in the WEB-INF/images folder, the user CANNOT access them directly. Remember, anything under the WEB-INF folder cannot be accessed directly by the user (ie., u cant do http://localhost/myapp/WEB-INF/images/test.jpg)

So, you could create a servlet that takes in a parameter (imageName) and fetches the image:

sample code:


Now create a servlet mapping to this serlvet:

Servlet: ImageServlet
Servlet Mapping: /image

Also create a filter mapping for the url /image

Filter: LoginFilter
URL Mapping: /image

So now when you access the image as http://localhost/image?imageName=pg.jpg ==> the login filter kicks in, does its security checks and then the imagesservlet kicks in and gets the image pg.jpg from the internal location and serves it to the user
[ June 05, 2006: Message edited by: Mike Corleone ]

MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
or alternatively u can move the images to a top level folder (outside WEB-INF) lets say "/myImages" folder.

you just need a filter mapping in this case:

filter: LoginFilter
filter url mapping: /myImages

and when you access http://localhost/myImages/pg.jpg - your loginfilter will check the credentials

This approach doesnt need a servlet to fetch the image.
Susan Monz

Joined: Aug 23, 2008
Posts: 27

I have a similar requirement. Can you send me a sample of your login filter.
I simply used an ImageServlet and directly accessed the image on my jsf.

The thing is it is not secure. Anyone can easily access the image by getting hold of the url + manupilating my passed param.
karl marbin

Joined: Sep 16, 2008
Posts: 1
get first the context path of the where the image is located. it is the best way to display image in JSF
I agree. Here's the link:
subject: Display images/ in WEB-INF
jQuery in Action, 3rd edition