Why use ServletContext.setAttribute()?
10,982
The servletContext.setAttribute()
is dynamic which can be set and reset during runtime.
Where as init-parameter
specified in web.xml
is static which will not change during the lifetime of application.
Example:
- The database properties like database name configuration propety. It will be mostly configured as context
init-param
- And if you want to set property value which derives based on runtime changes, like "last user logged in" or "number of failures" should be context attributes.
![Jayesh](https://i.stack.imgur.com/1YZhh.png?s=256&g=1)
Author by
Jayesh
Updated on July 19, 2022Comments
-
Jayesh almost 2 years
Why would we set ServletContext parameters using the
setAttribute()
method, as we can accomplish the same thing by setting parameters inweb.xml
and fetching them usinggetInitParameter()
?