• Post Reply Bookmark Topic Watch Topic
  • New Topic

Opening files from servlets

 
igwe kalu kalu ogba
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I've written an servlet which has to open some xml files and extract data. My question is, where do I place these files so that I can run my methods without entering a full directory path.

i.e, Say I have a method open(String filename), I can simply say open("Prox.xml"); instead of open("C:\java\Prox.xml");
Where can I place files in my servlet structure to be able to do this?
I need to know because the program I am writing can be installed anywhere, it can be to the E drive or C drive or whatever. Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the files are within the web app hierarchy, ServletContext.getRealPath() will help you find the absolute file path given the app-relative path.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
If the files are within the web app hierarchy, ServletContext.getRealPath() will help you find the absolute file path given the app-relative path.


If the app is deployed to run from an exploded file system.
If you are running it as a packed war file, getRealPath will return null.

I recommend setting up either a servlet init-param or a context init-param that points to the directory where you plan to keep these files unless they are going to be shipped with the app and never changed after deployment.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!