4

I know it it is easy to read the context-param values from web.xml with a Servlet.

But is it possible to read the value with a normal java class?

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
Jay
  • 245
  • 1
  • 5
  • 11
  • Are you going to provide your own servlets spec implementation? – Roman Sep 21 '10 at 16:15
  • The web app is mixed with servlets and old style jsp pages. I am trying to make the property java class can read the property file path from web.xml instead of a constant. – Jay Sep 21 '10 at 16:18
  • It is possible using File api. But why would somebody want to do it. – Ankit Bansal Sep 21 '10 at 16:19
  • @Roman I donot understand what you mean by my own servlets spec implementation... – Jay Sep 22 '10 at 14:26

2 Answers2

4

You can perse web.xml (with something like dom4j), but I guess that's not your point - a context-param does not make any sense in a non-servlet environment.

I assume you want to obtain the value of an init parameter in a class in your web application that is not a servlet, but is, say, a helper class.

You can - your entry point is always a servlet, so get the context-param value there and pass it as a method argument to the method you need.

In case this is configuration that you can afford to make static - load it only once, on init() of a servlet.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
0

If you have the Request object you can get the ServletContext from that and you can get the init parameters from the Context via the API.

user207421
  • 305,947
  • 44
  • 307
  • 483