Why can't servlets access a file with a relative path
Joined: Nov 29, 2000
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?
Author and all-around good cowpoke
Joined: Mar 22, 2000
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
That's a good idea. I've been searching for any solution to this one . Thanks!!!
kamal pasha shaikh
Joined: Aug 21, 2001
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.
Joined: Jun 13, 2000
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