【问题标题】:Why use ServletContext.setAttribute()?为什么使用 ServletContext.setAttribute()?
【发布时间】:2012-06-18 06:57:45
【问题描述】:

为什么我们要使用setAttribute() 方法设置ServletContext 参数,因为我们可以通过在web.xml 中设置参数并使用getInitParameter() 获取它们来完成同样的事情?

【问题讨论】:

    标签: servlets setattribute


    【解决方案1】:

    servletContext.setAttribute() 是动态的,可以在运行时设置和重置。

    web.xml 中指定的 init-parameter 是静态的,在应用程序的生命周期内不会改变。

    示例:

    • 数据库属性,如数据库名称配置属性。它将主要配置为上下文init-param
    • 如果您想设置基于运行时更改派生的属性值,例如“最后登录用户”或“失败次数”应该是上下文属性。

    【讨论】:

    • 对于 Servlet 2.5,ServletContext.getInitParameter() 是否会返回由 ServletContext.setAttribute() 设置的相同属性?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 2011-08-30
    • 2010-11-01
    • 2015-08-10
    • 2018-01-14
    • 2010-11-24
    • 2011-03-25
    相关资源
    最近更新 更多