title: Glass fish 4 alternatedocroot list of files for Prime Faces
body: <p>I am trying to store some images in a Glassfish 4 alternate doc root. The idea is to allow admin staff to save images that will show on an index page using the Prime Faces imageswitch. This is essentially an automatic slideshow. I'm trying to use the alternate doc root because any uploaded images may not survive an app redeploy with Glassfich</p>
  
  <p>My glassfish-web.xml is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd"&gt;
  &lt;glassfish-web-app error-url=""&gt;
    &lt;context-root&gt;realtyfreedom&lt;/context-root&gt;
      &lt;property description="Static Images" name="alternatedocroot_1" value="from=/static/images/* dir=/private/var/realtyfreedom" /&gt; 
  
    &lt;class-loader delegate="true"/&gt;
    &lt;jsp-config&gt;
      &lt;property name="keepgenerated" value="true"&gt;
        &lt;description&gt;Keep a copy of the generated servlet class' java code.&lt;/description&gt;
      &lt;/property&gt;
    &lt;/jsp-config&gt;
  &lt;/glassfish-web-app&gt;
  </code></pre>
  
  <p>My named bean looks like this:</p>
  
  <pre><code>package net.realtyfreedom.beans;
  
  import java.io.Serializable;
  import java.util.Arrays;
  import javax.enterprise.context.SessionScoped;
  import javax.faces.context.*;
  import javax.inject.Named;
  import net.realtyfreedom.utils.SlideRetriever;
  //import org.apache.log4j.*;
  import java.util.logging.Logger;
  
  @Named
  @SessionScoped
  public class SlideList implements Serializable
  {
  
     private final String[] images = new String[100];
     private final String facesContext = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
     //private final String imagePath = facesContext + "/images/";
     private final String imagePath = "/realtyfreedom/static/images/index.xhtml";
     private final Logger logger = Logger.getLogger(this.getClass().getName());
  
  
     public String getFacesContext()
     {
  
        logger.info("log4j: Faces Context is: " + facesContext);
        return imagePath;
     }
  
     public String[] getImages() {
        SlideRetriever sr = new SlideRetriever();
        return sr.getImages();
     }
     public String getImageNames() {
        SlideRetriever sr = new SlideRetriever();
        sr.init(imagePath);
        logger.info("App log array: sr: sr list" + Arrays.toString(sr.getImages()));
        String[] imageList =  sr.getImages();
  
        //logger.("info: image files: " + imageList.length + " " + Arrays.toString(imageList));
        return Arrays.toString(imageList);
     }
  }
  </code></pre>
  
  <p>Here is the utility class that retrieves the array of files</p>
  
  <pre><code>package net.realtyfreedom.utils;
  
  import java.io.File;
  import java.io.Serializable;
  import java.util.Arrays;
  import javax.faces.context.FacesContext;
  import java.util.logging.Logger;
  
  public class SlideRetriever implements Serializable
  {
  
     private String[] images = new String[100];
     private final String facesContext = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/resources/images/slideshow");
     //private final String imagePath = facesContext + "/resources/images/slideshow";
     private String imagePath = "";
     private final Logger logger = Logger.getLogger(this.getClass().getName());
  
     public SlideRetriever()
     {
  
     }
  
     public void init(String ip)
     {
        imagePath = ip;
        try
        {
           File folder = new File(imagePath);
           logger.info("App: Folder path is: " + imagePath);
           logger.info("App: Canonical Path : " + folder.getCanonicalPath());
  
           images = folder.list();
  
        } catch (Exception ex)
        {
           System.out.println(ex.getStackTrace());
        }
     }
  
     public String[] getImages()
     {
        logger.info("Debug: " + Arrays.toString(images));
        return images;
     }
  }
  </code></pre>
  
  <p>The result is that the string array images in the init() method of the SlideRetriever class returns null. However when I run this code in a console app, I get a list of file names. Also, I can use the image path along with an existing file in the static/images directory I can display an image in the browser.</p>
  
  <p>I saw a similar question for Glassfish 3, but I have not seen this question for Glassfish 4. In Glassfish 3, I think the resulting answer was for the developer to write images to the database. I would like to avoid this if possible. Any help would be greatly appreciated. And sorry for the rough state of the code.</p>