Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Servlets 为什么要使用ServletContext.setAttribute()?_Servlets_Setattribute - Fatal编程技术网

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
中指定的as
init参数是静态的,在应用程序的生命周期内不会更改

示例:

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

对于Servlet 2.5,ServletContext.getInitParameter()是否会返回ServletContext.setAttribute()设置的相同属性?