I need to display images which reside outside of deploy folder in web application using JSF <h:graphicimage> tag or HTML <img> tag. How can I achieve that?
I need to display images which reside outside of deploy folder in web application
Share
To the point, it has to be accessible by a public URL. Thus, the
<img src>must ultimately refer ahttp://URI, not something like afile://URI or so. Ultimately, the HTML source is executed at enduser’s machine and images are downloaded individually by the webbrowser during parsing the HTML source. When the webbrowser encounters afile://URI such asC:\path\to\image.png, then it will look in enduser’s own local disk file system for the image instead of the webserver’s one. This is obviously not going to work if the webbrowser runs at a physically different machine than the webserver.There are several ways to achieve this:
If you have full control over the images folder, then just drop the folder with all images, e.g.
/imagesdirectly in servletcontainer’s deploy folder, such as the/webappsfolder in case of Tomcat and/domains/domain1/applicationsfolder in case of GlassFish. No further configuration is necessary.Or, add a new webapp context to the server which points to the absolute disk file system location of the folder with those images. How to do that depends on the container used. The below examples assume that images are located in
/path/to/imagesand that you’d like to access them via http://…/images.In case of Tomcat, add the following new entry to Tomcat’s
/conf/server.xmlinside<Host>:In case of GlassFish, add the following entry to
/WEB-INF/glassfish-web.xml:In case of WildFly, add the following entry inside
<host name="default-host">of/standalone/configuration/standalone.xml…… and further down in
<handlers>entry of the very same<subsystem>as above<location>:Or, create a
Servletwhich streams the image from disk to response:If you happen to use OmniFaces, then the
FileServletmay be useful as it also takes into account head, caching and range requests.Or, use OmniFaces
<o:graphicImage>which supports a bean property returningbyte[]orInputStream:Or, use PrimeFaces
<p:graphicImage>which supports a bean method returning PrimeFaces-specificStreamedContent.For the first way and the Tomcat and WildFly approaches in second way, the images will be available by http://example.com/images/filename.ext and thus referencable in plain HTML as follows
For the GlassFish approach in second way and the third way, the images will be available by http://example.com/context/images/filename.ext and thus referencable in plain HTML as follows
or in JSF as follows (context path is automatically prepended)
For the OmniFaces approach in fourth way, reference it as follows
For the PrimeFaces approach in fifth way, reference it as follows:
Note that the example
#{bean}is@ApplicationScopedas it basically represents a stateless service. You can also make it@RequestScoped, but then the bean would be recreated on every single request, for nothing. You cannot make it@ViewScoped, because at the moment the browser needs to download the image, the server doesn’t create a JSF page. You can make it@SessionScoped, but then it’s saved in memory, for nothing.See also: