Why use ServletContext.setAttribute()?

。_饼干妹妹 提交于 2019-12-17 19:00:33

问题


Why would we set ServletContext parameters using the setAttribute() method, as we can accomplish the same thing by setting parameters in web.xml and fetching them using getInitParameter()?


回答1:


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.


来源:https://stackoverflow.com/questions/11046717/why-use-servletcontext-setattribute

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