wood burning stoves 2.0*
The moose likes Servlets and the fly likes Why can't servlets access a file with a relative path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why can Watch "Why can New topic
Author

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

MATT JACKSON
Greenhorn

Joined: Nov 29, 2000
Posts: 5
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

Joined: Mar 22, 2000
Posts: 12761
    
    5
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

Joined: Nov 29, 2000
Posts: 5
That's a good idea. I've been searching for any solution to this one . Thanks!!!
kamal pasha shaikh
Greenhorn

Joined: Aug 21, 2001
Posts: 1
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

Joined: Jun 13, 2000
Posts: 3340
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


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can't servlets access a file with a relative path
 
Similar Threads
2 jnlp files using the same extensions
Loading custom application config files
Accessing Files from a Web App
JASPER Compile Exception during JSP precompilation
Trusted and Signed Applet still gets java.io.filepermission read error