I am using Eclipse to create a Dynamic Web Project. My eclipse is in
/home/pc/eclipse
and My project is in /home/pc/workspace/MyWebProj
Now I placed the files in the above Project Directory. When I want to read any File from my code, It always searches the files in /home/pc/eclipse folder instead of /home/pc/workspace/MyWebProj, thus I am hardcoding the file path for time being.
Is there any configuration setting in Eclipse I am missing?
Please Suggest.
Thank You,
Tara Singh
I think what you need to do is read the file from the location of the
javax.servlet.ServletContextof the Web Application. If you usegetContextPath()you should get the working directory of the Web Application rather than the Java VM.You could also try loading any files using the
getResourceandgetResourceAsStreammethods from the same class.I was a little rushed when I wrote this so I will provide a little more info.
The best approach is to use the getResourceAsStream method. this allows you to access files within your webapp.
To get a file foo.txt from the root directory of the web application and print it to the response, you could do the following.