2 Replies Latest reply on Feb 14, 2013 9:02 PM by dvohra21

    completing relative path

      I have a relative path which points to a file in Web Content folder. I use below code to complete my relative path. It works at local but when I deploy it to the real server it doesn't work. I can't find the file.

      public String returnAbsolutePath(FacesContext facesContext, String pathAndFileName) {

      FacesContext fctx = facesContext.getCurrentInstance();
      ServletContext servletCtx = (ServletContext)fctx.getExternalContext().getContext();
      String imageDirPath = servletCtx.getRealPath("");
      String absolutePath = imageDirPath + pathAndFileName;
      return absolutePath ;


      Studio Edition Version

      Edited by: nonder on Feb 14, 2013 7:05 AM
        • 1. Re: completing relative path
          Frank Nimphius-Oracle

          from the JavaDocs


          java.lang.String getRealPath(java.lang.String path)

          Gets the real path corresponding to the given virtual path.

          For example, if path is equal to /index.html, this method will return the absolute file path on the server's filesystem to which a request of the form http://<host>:<port>/<contextPath>/index.html would be mapped, where <contextPath> corresponds to the context path of this ServletContext.

          The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators.

          Resources inside the /META-INF/resources directories of JAR files bundled in the application's /WEB-INF/lib directory must be considered only if the container has unpacked them from their containing JAR file, in which case the path to the unpacked location must be returned.

          This method returns null if the servlet container is unable to translate the given virtual path to a real path.

          path - the virtual path to be translated to a real path
          the real path, or null if the translation cannot be performed

          So instead of

          String imageDirPath = servletCtx.getRealPath("");

          wouldn't it mean you need to use

          String imageDirPath = servletCtx.getRealPath("/");

          Also keep in mind that if e.g. your development is on Windows and the production server is UNIX, path and case may be different

          • 2. Re: completing relative path
            If the absolute/relative paths are obtained to get an image use the resource handling new feature in JSF 2.0.

            Create a WebContent/resources/images directory and add the images to the images directory. In a JSF page an image (logo.jpg for example) may be used with
            <h:graphicImage library="images" name="logo.jpg"></h:graphicImage>