It's not a secret anymore!*
The moose likes JSF and the fly likes How to read file in war ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "How to read file in war ? " Watch "How to read file in war ? " New topic
Author

How to read file in war ?

chen young
Ranch Hand

Joined: Sep 09, 2005
Posts: 188
Hi,

I need to read xml file that is stored inside the jsf web application.

I am saving all the xml files in the following path:
c:\Users\me\NetBeansProjects\manager\src\main\resources\com\cron\dis\resources\template\my_file.xml

after building the war I will get something like this:
..\target\manager-1.0-SNAPSHOT\WEB-INF\classes\com\cron\dis\resources\template\my_file.xml

In order to load the file I am using the Class getResource like this:


I try any possible path combination that I can think and still the URI is empty 

I don’t know what am I doing wrong here ??

This is a jsf web application and I am using NetBeans.

Could someone please explain how can I load the file ??

Any help would be most appreciated

Thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15951
    
  19

Your IDE should never determine how you code a program. The IDE won't be available on the production server. Or at least I hope not!

The proper way to read a file that's been bundled into a WAR is to treat it as a WAR resource. I just happen to have some code that will make that easier:


Note that I placed this function in a discrete utilities class. That way my main application code is spared the horror of being laced with framework-specific code.

Note that you should never write to a file in a WAR. Resources in a WAR should be treated as read-only. If you need to write or update, use a directory that's external to both the WAR and to the webapp server.


Customer surveys are for companies who didn't pay proper attention to begin with.
chen young
Ranch Hand

Joined: Sep 09, 2005
Posts: 188
Thank you very much

This is very helpful.
 
jQuery in Action, 2nd edition
 
subject: How to read file in war ?
 
Similar Threads
Heads First - Web App Deployment - Question 14 (Page 615)
true or false, getResource sematic
Regarding reading a xml file in a war file
to find the workspace path at runtime to write the file
Reading an XML file in a war file