Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Reading a File in a JSF project.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there.

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

James
 
Saloon Keeper
Posts: 24871
174
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic