Link to home
Start Free TrialLog in
Avatar of michaelzhou23
michaelzhou23

asked on

How to access local file system resource from a Servlet container

Hi experts,
  I have a local properties file that I want to read within a class to be deployed in a Servlet container, do you know of any standard way of doing this?
  Thanks!

Cheers,
Michael
Avatar of for_yan
for_yan
Flag of United States of America image

If you mean the file is local on the server - I don't think reading properties file from a servlet
would be any different than reading any file or say any property file from any other Java
program. This is say one exmple of reading properties file:

http://www.zparacha.com/how-to-read-properties-file-in-java/
Avatar of Mick Barry
if its inside the classes (or lib) then you can use the class loader
http://helpdesk.objects.com.au/java/how-to-get-url-to-a-resource-in-same-directory-as-a-class
Avatar of michaelzhou23
michaelzhou23

ASKER

Thanks for the quick response.
Sorry for the confusion, I will just make the point clearer. I'm creating a JAR file that contains a class to read a local file. The file is on the same machine(file system) with the Web app server. The JAR is to be used within a Web app. Specifically, what protocol's (file:/// ?) should I use to access the file in the file system rather than in a Web environment (Servlet container)?
I would probably not tell you what is the recommended way, if ther is one, but from experience I know
that you don't need to use any protocol - you can read files form the file system on your web server
as you read any files in Java using all possible methods of reading files. Servlet is the java program like any other java program
and can deal with fileseither  within  the web server root document tree or outside this tree quite freely.
It is if you want to package your property file with your web application to be deployed on any server later
than the way objects suggested with class loader becomes essential.
> Specifically, what protocol's (file:/// ?) should I use to access the file in the file system rather than in a Web environment (Servlet container)?

yes, the file protocol.
Thanks guys for the suggestion! The "file:/" protocol seems to work!
Now, to make my coding more elegant, I want to embed the text file into my JAR file so it is portable along with the JAR. How do I pack it into a JAR and access it from the class in the same JAR?
Thanks!

This is what you can see in the link, which objects posted above.

This one is a little bit more elaborated version of the same:
http://snippets.dzone.com/posts/show/813
You need to put the jar in WEB-INF/lib for that to work
ASKER CERTIFIED SOLUTION
Avatar of Plk_In_EE
Plk_In_EE
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial