1

i'm working in java web application project with jsf 2.2.4 framework and glassfish 4 server. i have directory structure like this in my project:

enter image description here

how do i access the gambar directory and file inside the gambar directory ? note : the gambar directory is outside war (i want to put it outside war).

many thanks for the answer

Ahmet Kakıcı
  • 6,294
  • 4
  • 37
  • 49
  • Where does that folder end up in the built WAR file? Export project as WAR and extract the WAR using some zip tool and look. If it isn't there, then there's no way to get it from inside the program, obviously ;) In that case, you'd need to take a step back and think/ask first how to get that folder to end up in the build. – BalusC Nov 28 '13 at 17:03
  • Where does that folder end up in the built WAR file? it's out build war. could i add it manually to the build folder like http://s9.postimg.org/jyx23mb5b/image.jpg ?. you're right BalusC how to get that folder to end up in the build ? i remember when i used xampp (php projects) i just put the folder to the application folder in htdocs and access it via localhost/application/folder. – Lukman Munaji Nov 29 '13 at 07:14

1 Answers1

2

There is no direct way to load a file from outside of servlet context in servlet or framework such as JSF. You can however provide the absolute folder path in the context param of web application and then read it from within your JSF bean

<web-app>
..................
    <context-param>
        <param-name>myfolderpath</param-name>
        <param-value>D:/myfolder</param-value>
    </context-param>
..................
</web-app>

and thereafter reading it

FacesContext ctx = FacesContext.getCurrentInstance();
String myConstantValue =
    ctx.getExternalContext().getInitParameter("myfolderpath");

Another approach is to pass your folder location in System properties while the server starts and then access it in your JSF bean.

However this design should be looked into more carefully. Some good discussion on its demerits here and here

Community
  • 1
  • 1
Shailendra
  • 8,874
  • 2
  • 28
  • 37
  • return null... could i use context param in web-inf with value from local disk (d:\netbeans project\webproject\gambar) ? how to do that ? – Lukman Munaji Nov 28 '13 at 16:58
  • Have a look at updated answer or http://stackoverflow.com/questions/6523193/jsf-2-how-can-i-get-a-context-param-value-from-web-xml-using-jsf-el – Shailendra Nov 28 '13 at 18:13