Why use ServletContext.setAttribute ()?

Why do we need to set the ServletContext parameters using the setAttribute() method, since we can do the same by setting the parameters in web.xml and retrieving them with getInitParameter() ?

+3
servlets
Jun 15 2018-12-12T00:
source share
1 answer

servletContext.setAttribute() is dynamic, which can be set and reset at runtime.

Where indicated, the init-parameter specified in web.xml is static, which will not change throughout the life of the application.

Example:

  • Database properties, such as propety database configuration. It will be mainly configured as init-param context
  • And if you want to set the value of a property that occurs based on changes at runtime, for example, "last user logged in" or "number of failures" should be context attributes.
+12
Jun 15 '12 at 8:01
source share



All Articles