File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » JSF
Bookmark "How to read file in war ? " Watch "How to read file in war ? " New topic

How to read file in war ?

chen young
Ranch Hand

Joined: Sep 09, 2005
Posts: 197

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:

after building the war I will get something like this:

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

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

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.

An IDE is no substitute for an Intelligent Developer.
chen young
Ranch Hand

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

This is very helpful.
I agree. Here's the link:
subject: How to read file in war ?
It's not a secret anymore!