• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I cannot access ordinary file from servlets by giving relative Path

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually i want to access .txt file from servlet.
When i give the absoulte path it takes the file .otherwise it's giving the error . FilenotFound Exception . I am using Jrun web server.If anybody knows the answer please tell me immediately as i am executing some important project
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Relative path makes an assumption about the "current directory" - in a servlet you have no control over the current directory.
Seems to me your choices are:
1. Use servlet init parameters to specify a base path for your working files.
2. Use the ServletContext methods for getting the real path from a application-relative path.
Incidently - "please tell me immediately" doesn't do the slightest good around here - people either answer or they don't.
Bill
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use this code on several websites (Tomcat & Resin driven) for reading content for the pages. I think you will get the idea from it
URL url = getServletContext().getResource("/siteText/"+pageName+".txt");
InputStream is = url.openStream();
if (is != null){
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String s;
while ((s = br.readLine()) != null){
sb.append(s);
sb.append("\n");
}
br.close();
is.close();
}
Hope this helps
 
eat bricks! HA! And here's another one! And a tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic