File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Reading a File in a JSF project. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Reading a File in a JSF project." Watch "Reading a File in a JSF project." New topic

Reading a File in a JSF project.

Tiago Domingues

Joined: Jan 26, 2011
Posts: 1
Hi there.

I can´t read a txt file in the WEB-INF folder, with FacesContext.getCurrentInstance.getExternalContext.getResourceAsStream...
Am i missing something ?

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

Well, to be pedantic, that wouldn't be a literal file in any case. According to the J2EE spec, a deployed webapp is a WAR file, and this would just be one of the objects within the WAR that, if the WAR is unzipped would become an independent file.

There are 2 most likely causes for your problem. One is if you're asking for a FacesContext outside of the JSF context. In which case, you'd get a NullPointer.

The other is failure to specifiy the resource location properly on the getResourceAsStream request. The resource location is relative to the WAR, not the URL, so the proper value would be "/WEB-INF/myfile.txt" or something similar.

And, although you're evidently not intending to do so, I should repeat that old warning that WRITING to anything in a WAR is something that should not be done!

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: Reading a File in a JSF project.
jQuery in Action, 3rd edition