• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Reading a File in Servlet from Web App Directory

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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().
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic