This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Reading a File in Servlet from Web App Directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Reading a File in Servlet from Web App Directory" Watch "Reading a File in Servlet from Web App Directory" New topic
Author

Reading a File in Servlet from Web App Directory

Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
I have what I hope is a simple question. I was wondering why I am not able to read a .xml file into a Servlet from a directory inside the web application but can read it into the servlet from a directory that is outside of web application? Something special going on with web app directories and file access?

I specify the path like /xml/theXmlFile.xml which is just off of the root of the web app and it always returns null, but if i specify path to file like /home/myfiles/xml/theXmlFile.xml which is another directory outside of the web app it returns the file.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

By starting the path with a "/" you are telling it to start at the file system root.

To read files relative to the base of the web app, you need to use one of the mechanisms provided for that. Read up on getRealPath() and getResourceAsStream().


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Warning: getRealPath() returns the path on the file system leading to your application if the app is running as an exploded file system. On the other hand if your app has been deployed as war file, getRealPath() will not work.

Hence, its better to go for getResourceAsStream().
Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
I used getRealPath() and its not working. Here's my real path to the files:

xml file path: /opt/netbeans/SunAppServer8.1/domains/domain1/applications/j2ee-modules/Dev-WebModule/xml/article.xml

xsl file path: /opt/netbeans/SunAppServer8.1/domains/domain1/applications/j2ee-modules/Dev-WebModule/xsl/article.xsl

I'm not sure what Netbeans does when running the project as far as creating a deployed war etc..But it does deploy via war as the standard way to move the app to another server.

Any suggestions for J2EE IDE on Linux?

I will try getResourceAsStream() Thanks.
[ January 25, 2006: Message edited by: Stu Higgs ]
Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
Ok, getRealPath() works. I had a syntax error in my xsl file. This is ok solution for now because app will not be deployed as war file.

Using getResourceAsStream(), I have the stream and am reading from it. How it is going to fit into my program is a whole other story

I'm sure I'll have questions after I try to implement it into my program.

Thanks for your help.
[ January 25, 2006: Message edited by: Stu Higgs ]
Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
Warning: getRealPath() returns the path on the file system leading to your application if the app is running as an exploded file system. On the other hand if your app has been deployed as war file, getRealPath() will not work.

Hence, its better to go for getResourceAsStream().



I came across this very problem when deploying a WAR to Weblogic for the same code I was using when I posted the original message in this thread.

I Still am having problems between different servers(Weblogic and Tomcat) where each server reponds differently and yields different outcomes for the same code so for now I have implemented different code for each server

See Related Weblogic Thread for more information:

Weblogic getRealPath vs. getResourceAsStream()
[ August 13, 2006: Message edited by: Stu Higgs ]
 
Consider Paul's rocket mass heater.
 
subject: Reading a File in Servlet from Web App Directory
 
Similar Threads
how can i put configurations files outside war file
File reading from any path
get servlet uri in init
Tomcat webapp
Writing a File and Request Dispatcher Forward