Servlet-spec: <context-param> vs <env-entry> in web.xml?

霸气de小男生 提交于 2019-11-30 15:07:11

问题


Why does the Servlet specification define two distinct ways (context parameters and environment entries) to provide a web application with configuration parameters?

What are the respective advantages of these approaches (when should which be preferred)?


回答1:


Environment entries are available via JNDI which may be useful when you don't have a ServletContext directly at hands, such as in EJBs. The one in the web.xml is actually the last in precedence chain as to overridding environment entires. They are usually definied in server's own configuration. So if one intends to override a server-specified environment entry from the webapp on, then that could be done via web.xml.

Context parameters are really specific to the webapp itself. They are only available when you have a ServletContext directly at hands, usually only inside filters, servlets (and inherently also JSPs via ${initParam.someName} in EL) and listeners. They are supposed to be used to provide configuration parameters for filters, servlets and/or listeners running in the webapplication. It wouldn't make much sense to provide them by JNDI which is an overcomplicated process for the simple purpose.



来源:https://stackoverflow.com/questions/13196868/servlet-spec-context-param-vs-env-entry-in-web-xml

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