This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I cannot access ordinary file from servlets by giving relative Path

 
Pavitdeep Singh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • 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
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic