Win a copy of Clojure in Action this week in the Clojure 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
  • 0
  • 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: 13048
6
  • 0
  • 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
  • 0
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic