Search code examples
servletsjndicontext-param

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


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)?


Solution

  • 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.