How to gain access to a ServletContext instance from any method?

一世执手 提交于 2020-01-13 16:25:06

问题


Is there any way to get access to the ServletContext from a method without passing the ServletContext as an argument?

I need to have a generic Configuration class that can work on several environments and load the config in a way depending on the environment.

For example, when in a web app, i need to get the configuration from either web.xml or a config file stored in WEB-INF.

But in order to know if the app is running in a web app i need to gain access to the ServletContext somehow.

Any ideas?


回答1:


Have a class that holds servlet context as static field initilize it from ServletContextListener



来源:https://stackoverflow.com/questions/5646374/how-to-gain-access-to-a-servletcontext-instance-from-any-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!