开发者

How to read context-param values from web.xml with a normal Java class [duplicate]

开发者 https://www.devze.com 2023-01-16 20:58 出处:网络
This question already has answers here: How can I read context parameter/web.xml values in a non-servlet java file?
This question already has answers here: How can I read context parameter/web.xml values in a non-servlet java file? (5 answers) Closed 6 years ago.

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

But is it p开发者_JAVA百科ossible to read the value with a normal java class?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消