• Post Reply Bookmark Topic Watch Topic
  • New Topic

solution to read a file from FacesContext  RSS feed

 
rajesh babu Y
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
 
Tim Holloway
Bartender
Posts: 18781
74
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: 18781
74
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!