I have a servlet which has to read data from custom.xml file. while i can read the data from a normal class using <code> BufferedReader in = new BufferedReader(new FileReader("custom.xml")); </code> with out any error. But i get custom.xml not found in the class path when i try to read the same from a servlet. where should i keep this custom.xml for a servlet to be accessed under class path?
Relative file paths are meaningless in web applications because there is no concept of a "current directory".
You will need to locate the file either by using an absolute file path (provided by a context param, property file or some other means other than hard-coding), or if the file is located within the web application, you can employ getResourceAsStream() or getRalPath() to locate the file.
My point is that the problem with your code is that you have bound it to a specific environment: one in which the current directory exists and contains the file. You've also hard-coded the file name into the method. These decisions severely limit the reusability and flexibility of the code; as you have seen.
Rather, re-factor the method to recieve either an absolute file path, or an InputStream. That way, it is the responsibility of the environmnet-specific calling code to fnd the file and supply it to the routine which is now flexible and reusable.
Joined: May 14, 2005
I used the absolute path temporarily and it works fine.