• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

solution to read a file from FacesContext

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 27807
196
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 27807
196
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
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.
reply
    Bookmark Topic Watch Topic
  • New Topic