jsf - Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag -


i need display images reside outside of deploy folder in web application using jsf <h:graphicimage> tag or html <img> tag. how can achieve that?

to point, has accessible public url. thus, <img src> must refer http:// uri, not file:// uri or so. ultimately, html source executed @ enduser's machine , images downloaded individually webbrowser during parsing html source. when webbrowser encounters file:// uri such c:\path\to\image.png, in enduser's own local disk file system image instead of webserver's one. not going work if webbrowser runs @ physically different machine webserver.

there several ways achieve this:

  1. if have full control on images folder, drop folder images, e.g. /images directly in servletcontainer's deploy folder, such /webapps folder in case of tomcat , /domains/domain1/applications folder in case of glassfish. no further configuration necessary.


  2. or, add new webapp context server points absolute disk file system location of folder images. how depends on container used. below examples assume images located in /path/to/images , you'd access them via http://.../images.

    in case of tomcat, add following new entry tomcat's /conf/server.xml inside <host>:

    <context docbase="/path/to/images" path="/images" /> 

    in case of glassfish, add following entry /web-inf/glassfish-web.xml:

    <property name="alternatedocroot_1" value="from=/images/* dir=/path/to" /> 

    in case of wildfly, add following entry inside <host name="default-host"> of /standalone/configuration/standalone.xml ...

    <location name="/images" handler="images-content" /> 

    ... , further down in <handlers> entry of same <subsystem> above <location>:

    <file name="images-content" path="/path/to/images" /> 

  3. or, create servlet streams image disk response:

    @webservlet("/images/*") public class imageservlet extends httpservlet {      protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {         string filename = request.getpathinfo().substring(1);         file file = new file("/path/to/images", filename);         response.setheader("content-type", getservletcontext().getmimetype(filename));         response.setheader("content-length", string.valueof(file.length()));         response.setheader("content-disposition", "inline; filename=\"" + filename + "\"");         files.copy(file.topath(), response.getoutputstream());     }  } 

    if happen use omnifaces, fileservlet may useful takes account head, caching , range requests.


  4. or, use omnifaces <o:graphicimage> supports bean property returning byte[] or inputstream:

    public inputstream getimage(string filename) {     return new fileinputstream(new file("/path/to/images", filename)); } 

  5. or, use primefaces <p:graphicimage> supports bean method returning primefaces-specific streamedcontent.

    public streamedcontent getimage() throws ioexception {     facescontext context = facescontext.getcurrentinstance();      if (context.getcurrentphaseid() == phaseid.render_response) {         // so, we're rendering view. return stub streamedcontent generate right url.         return new defaultstreamedcontent();     }     else {         // so, browser requesting image. return real streamedcontent image bytes.         string filename = context.getexternalcontext().getrequestparametermap().get("filename");         return new defaultstreamedcontent(new fileinputstream(new file("/path/to/images", filename)));     } } 

for first way , tomcat , wildfly approaches in second way, images available http://example.com/images/filename.ext , referencable in plain html follows

<img src="/images/filename.ext" /> 

for glassfish approach in second way , third way, images available http://example.com/context/images/filename.ext , referencable in plain html follows

<img src="#{request.contextpath}/images/filename.ext" /> 

or in jsf follows (context path automatically prepended)

<h:graphicimage value="/images/filename.ext" /> 

for omnifaces approach in fourth way, reference follows

<o:graphicimage value="#{bean.getimage('filename.ext')}" /> 

for primefaces approach in fifth way, reference follows:

<p:graphicimage value="#{bean.image}">     <f:param name="filename" value="filename.ext" /> </p:graphicimage> 

see also:


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -