0

i need to access a configuration file with some properties from a class inside a webapp.

The webapp consist of a servlet that uses another class.

In that class i want to access a configuration file.

Which is the best way?

3

2 Answers 2

2

Use Properties file and read it from ContextListener and store it in some application wide accessible map it properties are not too many.

Also you can use web-param

Sign up to request clarification or add additional context in comments.

Comments

1

you can put a properties file under your WEB-INF/classes directory. in that way it wil be visible on the classpath, then in your java class you can use :

 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("your_file.properties");

3 Comments

Fine for a quick & dirty "hello world", what about dependency injection and testability?
@jglatre, please elaborate with example, i think this is perfectly fine for normal use.
It depends on your quality standards. I'd rather declare a public void setProperties(Properties props) method, wich lets me use the component in many different environments, for instance: in a JUnit test (I don't have to bother about external files, just create new Properties() on the fly), or in a Spring context (I can easily inject values in a XML beans file). Furthermore I can easily swap from one config file name to another, or even change file format, without affecting my component.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.