Solved

How to access local file system resource from a Servlet container

Posted on 2011-03-03
9
938 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:michaelzhou23
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35033859
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/
0
 
LVL 92

Expert Comment

by:objects
ID: 35033860
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
0
 

Author Comment

by:michaelzhou23
ID: 35033897
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)?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 47

Expert Comment

by:for_yan
ID: 35033919
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 35033979
> 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.
0
 

Author Comment

by:michaelzhou23
ID: 35034275
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!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35034326

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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35034761
You need to put the jar in WEB-INF/lib for that to work
0
 
LVL 5

Accepted Solution

by:
Plk_In_EE earned 125 total points
ID: 35056047
Hi there
You can use zip all classes into jar using export as jar in eclipse Or jar utility in java . make sure you place properties file.,tld files resources path rightly
thank you
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle SQL syntax check  without executing 6 75
SHA2 certs for IIS AND Java? 2 113
hibernate example using maven 12 59
Unhandled exception type Exception 18 42
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question