Servlets 为什么要使用ServletContext.setAttribute()?
为什么我们要使用Servlets 为什么要使用ServletContext.setAttribute()?,servlets,setattribute,Servlets,Setattribute,为什么我们要使用setAttribute()方法设置ServletContext参数,因为我们可以通过在web.xml中设置参数并使用getInitParameter()获取参数来完成同样的事情?ServletContext.setAttribute()是动态的,可以在运行时进行设置和重置 其中,web.xml中指定的asinit参数是静态的,在应用程序的生命周期内不会更改 示例: 数据库属性,如数据库名称配置属性。它将主要配置为contextinit param 如果您想设置基于运行时更改派
setAttribute()
方法设置ServletContext参数,因为我们可以通过在web.xml
中设置参数并使用getInitParameter()
获取参数来完成同样的事情?ServletContext.setAttribute()
是动态的,可以在运行时进行设置和重置
其中,web.xml
中指定的asinit参数是静态的,在应用程序的生命周期内不会更改
示例:
- 数据库属性,如数据库名称配置属性。它将主要配置为context
init param
- 如果您想设置基于运行时更改派生的属性值,例如“上次用户登录”或“失败次数”应该是上下文属性
对于Servlet 2.5,ServletContext.getInitParameter()是否会返回ServletContext.setAttribute()设置的相同属性?