Here’s a review of the key differences between context init parameters and servlet init parameters. Pay special attention to the fact that they’re both referred to as init parameters, even though only servlet init parameters have the word “init” in the DD configuration.
Context init parameters | Servlet init parameters |
---|---|
Deployment Descriptor | |
Within the <web-app> element but NOT within a specific <servlet> element <web-app ...> <context-param> <param-name>foo</param-name> <param-value>bar</param-value> </context-param> <!-- other stuff including servlet declarations --> </web-app> | Within the <servlet> element for each specific servlet <servlet> <servlet-name> BeerParamTests </servlet-name> <servlet-class> TestInitParams </servlet-class> <init-param> <param-name>foo</param-name> <param-value>bar</param-value> </init-param> <!-- other stuff --> </servlet> |
Servlet Code | |
getServletContext().getInitParameter("foo"); | getServletConfig().getInitParameter("foo"); |
Availability | |
To any servlets and JSPs that are part of this web app. | To only the servlet for which the <init-param> was configured. |
(Although the servlet can choose to make it more widely available by storing it in an attribute.) |