3

Is it possible for a web application to access its own deployed folder. I am using JSF 1.2 framework and i need to access some of the files which i have it in the deployed directory.

Does JSF has any in built method to give us the deployed folder and the files in it?

jmj
  • 237,923
  • 42
  • 401
  • 438
R K
  • 1,283
  • 1
  • 14
  • 41

3 Answers3

2

Use ExternalContext.getResourcePaths("/"). In a servlet container, this will delegate to ServletContext.getResoucePaths(String). As the documentation notes:

For example, for a web application containing:

/welcome.html
/catalog/index.html
/catalog/products.html
/catalog/offers/books.html
/catalog/offers/music.html
/customer/login.jsp
/WEB-INF/web.xml
/WEB-INF/classes/com.acme.OrderServlet.class
/WEB-INF/lib/catalog.jar!/META-INF/resources/catalog/moreOffers/books.html

getResourcePaths("/") would return {"/welcome.html", "/catalog/", "/customer/", "/WEB-INF/"}, and getResourcePaths("/catalog/") would return {"/catalog/index.html", "/catalog/products.html", "/catalog/offers/", "/catalog/moreOffers/"}.

For portable code, do not assume you can access resources via the file system:

This method (getResource(String)) allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.

McDowell
  • 107,573
  • 31
  • 204
  • 267
1

You can get access to resources in the classpath, but the servlet API does not guarantee their physical representation. In other words, if you deploy a WAR file, the container may explode the WAR file or keep it as is, or do something completely different depending on its needs.

In this particular context it mean that you introduce a subtle container dependency by assuming that a web application is deployed to a folder, which you should be very careful about.

If all you need, however, is to get some items you have in the classpath you should have a look at this question: Getting the inputstream from a classpath resource (XML file)

Community
  • 1
  • 1
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
0

FacesContext.getCurrentInstance().getExternalContext().getResource("/").toString();

you will get the path to your WEB-INF/classes dir traverse using parent(); method from File class to get the location

jmj
  • 237,923
  • 42
  • 401
  • 438
  • I am using tomcat server integrated with eclipse. The deployed folder path i get is "jndi:/localhost/AppName/". With this path i could not able to retrieve the files, as the listFiles attribute returns 'null' for the file object created with this path!! – R K Apr 25 '11 at 09:31
  • did `FacesContext.getCurrentInstance().getExternalContext().getResource("/").toString();` returned you `jndi:/localhost/AppName/` ? – jmj Apr 25 '11 at 09:34
  • yes. it returned "jndi:/localhost/AppName/". I am not sure is it because of tomcat integrated with eclipse! – R K Apr 25 '11 at 09:36
  • you can also get this using `MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()` – jmj Apr 25 '11 at 09:45