Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3602772
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:47:19+00:00 2026-05-18T20:47:19+00:00

I need to display images which reside outside of deploy folder in web application

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:47:20+00:00Added an answer on May 18, 2026 at 8:47 pm

    To the point, it has to be accessible by a public URL. Thus, the <img src> must ultimately refer a http:// URI, not something like a file:// 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 a file:// URI such as C:\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:

    1. If you have full control over the images folder, then just drop the folder with all images, e.g. /images directly in servletcontainer’s deploy folder, such as the /webapps folder in case of Tomcat and /domains/domain1/applications folder in case of GlassFish. No further configuration is necessary.


    2. 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/images and that you’d like to access them via http://…/images.

      In case of Tomcat, add the following new entry to Tomcat’s /conf/server.xml inside <Host>:

      <Context docBase="/path/to/images" path="/images" />
      

      In case of GlassFish, add the following entry to /WEB-INF/glassfish-web.xml:

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

      In case of WildFly, add the following entry inside <host name="default-host"> of /standalone/configuration/standalone.xml …

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

      … and further down in <handlers> entry of the very same <subsystem> as above <location>:

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

    3. Or, create a Servlet which streams the image from disk to 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 you happen to use OmniFaces, then the FileServlet may be useful as it also takes into account head, caching and range requests.


    4. Or, use OmniFaces <o:graphicImage> which supports a bean property returning byte[] or InputStream:

      @Named
      @ApplicationScoped
      public class Bean {
      
          public InputStream getImage(String filename) {
              return new FileInputStream(new File("/path/to/images", filename));
          }
      }
      

    5. Or, use PrimeFaces <p:graphicImage> which supports a bean method returning PrimeFaces-specific StreamedContent.

      @Named
      @ApplicationScoped
      public class Bean {
      
          public StreamedContent getImage() throws IOException {
              FacesContext context = FacesContext.getCurrentInstance();
      
              if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
                  // So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
                  return new DefaultStreamedContent();
              }
              else {
                  // So, browser is requesting the image. Return a real StreamedContent with the image bytes.
                  String filename = context.getExternalContext().getRequestParameterMap().get("filename");
                  return new DefaultStreamedContent(new FileInputStream(new File("/path/to/images", filename)));
              }
          }
      }
      

    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

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

    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

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

    or in JSF as follows (context path is automatically prepended)

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

    For the OmniFaces approach in fourth way, reference it as follows

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

    For the PrimeFaces approach in fifth way, reference it as follows:

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

    Note that the example #{bean} is @ApplicationScoped as 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:

    • Recommended way to save uploaded files in a servlet application
    • Simplest way to serve static data from outside the application server in a Java web application
    • Abstract template for a static resource servlet (supporting HTTP caching)
    • Show image as byte[] from database as graphic image in JSF page
    • Display dynamic image from database with p:graphicImage and StreamedContent
    • How to choose the right bean scope?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to display 2d images in opengl using textures. The image dimensions are
I am developing an android app in which i need to display images after
In my application I need to display questions along with images in Android. I
I need to display some images which can be scrolled horizontally. Only one image
I have a requirement where i need to display 1000 images which will take
Experts, I'm developing an application in Windows phone, Where i need to display images
I need to display an image, which I've done without problems before, but today
I display set of images(small). I need to show the larger image(300*300) at some
i am developing one application with map view i need display the weather depends
I need to display an error message on rejecting a drop in my application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.