This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

solution to read a file from FacesContext  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

i got a problem while reading a file in context root.

My application having war and jar file as ear with /lmt context root

when i tried to read files inside web-inf foleder like this

String path = (String)FacesContext.getCurrentInstance().getExternalContext().getResource("reports\templates").getPath();

i got NullPointerException from FacesServlet. report\templates are folders under WEB-INF folder in war file

can you please give me a response.

Thanks in Advance,
Rajesh Yarlagadda
 
Bartender
Posts: 19660
92
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even under Windows it's a bad idea to use backslashes in filename paths in Java. Use forward slashes and you'll be OS-independent. More importantly, you won't make mistakes like the one you made, which was failure to escape the filename's backslash character.

Since in Java, backslash is an escape character, you have to escape it: "reports\\templates". But "reports/templates" is much tidier. And safer.

Incidentally, if you want to read that file, you can save some work by using the "getResourceAsStream" method instead of getting the file and then opening a stream resource on the file to read it.

But NEVER write to anything inside a WAR! If you have to write a file, put it somewhere outside the WAR in the regular filesystem!
 
rajesh babu Y
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

Thanks you, my intention is, my folder structure is like this in war file.

WEB-INF/reports/templates/report.xml

i hard coded xml file name. but from ear application context root is /lmt.

give way how i can get the xml file which was located in war WEB-INF/reports/templates/xml from my application context root.

Thanks in Advance,
Rajesh Yarlagadda
 
Tim Holloway
Bartender
Posts: 19660
92
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The resource path is the relative path within the WAR. So it's like "WEB-INF/reports/templates/xml/template1.xml".

In an EAR, each WAR has its own resource path, so you can't get resources belonging to one WAR from another WAR.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!