Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why can't servlets access a file with a relative path

 
MATT JACKSON
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have servlets that use properties files in the init method.
I want access the files with a pth relative to the class file but I can't do that. I can only hard code the fullpath name to the file. Any Ideas why that is? Or any ideas about how to circumvent that?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats just the way it work, eh.
Use the web.xml file for your application to set a parameter that is read in the servlet init method to avoid having to hard code a path. (Thats assuming you are using an engine that is API 2.2 or later). Here is an example:
<servlet><servlet-name>Questionnaire</servlet-name>
<servlet-class>com.XmlEcomBook.Chap07.QuestionnaireServ
</servlet-class>
<init-param>
<param-name>homedir</param-name>
<param-value>e:\\scripts\\questionnaire</param-value>
</init-param>
</servlet>
Bill
 
MATT JACKSON
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a good idea. I've been searching for any solution to this one . Thanks!!!
 
kamal pasha shaikh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi bill,
please tell me how will the servlet identify where the xml file is kept if u r not specifying the absolute path.just as we had to specify the full hard coded path for the properties file so that servlet can access it and then read the key-value pairs from it,here we have a xml file instead of a properties file and this xml file will contain the path of the properties file as i understand.but again the problem is how will the servlet read the xml file using the relative path because that was the actual problem and if the servlet is able to read the xml file without giving the absolute path,then it should also be able to read the properties file without giving the absolute path.
well,please correct me if im wrong as i have stuck up with the same problem.
thanks in advance.
bye.
kamal.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the property file exists under your webapp, you can use ServletContext to locate and load the file.
Example: web.properties is under /WEB-INF/properties for your webapp




------------------
Hope This Helps
Carl Trusiak, SCJP2
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic