对象作用域与servlet事件监听器
2016-10-15 17:24
369 查看
1.对象作用域
我们已经学到在servlet里可以用一个名字绑定一个对象,并且在应用中传递和使用这个对象。这个名字和对象所构成的名字/值被称为属性。这里所谓的属性就是一个对象。被设置(绑定)到另外三个对象:servletcontect、httpservletRequest、HttpSession。
作用域:谁能看到并使用这个属性,以及他能存活多久。
2.回话作用域
ServletContext应用上下文
设置在ServletContext对象中的属性可以被Web应用中的任何一部分来访问
3.请求作用域
系统的资源消耗
属性可以保存在请求作用域范围中
存储时间比上下文作用域和会话作用域短。在请求结束后,对象就会被垃圾回收
在请求作用域范围之外就不能进行访问
监听器概述
监听session,request,application这三个对象里存取数据的变化
监听器对象可以在事情发生前、发生后可以做一些必要的处理
Servlet监听器主要目的是给Web应用增加事件处理机制,以便更好地监视和控制Web应用的状态变化
监听器分类
监听Web应用程序范围内的事件
Web应用启动和销毁事件
Web应用程序的属性发生改变的事件(包括增加、删除、修改)。
定义了ServletContextListener和ServletContextAttributeListener两个接口
ServletContextListener接口用于监听Web应用程序启动和销毁的事件
contextInitialized(ServletContextEvent sce):通知正在接受的对象,应用程序已经被加载及初始化
contextDestroyed(ServletContextEvent sce):通知正在接受的对象,应用程序已经被销毁
ServletContextAttributeListener接口
监听WEB应用属性改变的事件,包括:增加属性、删除属性、修改属性
attributeAdded(ServletContextAttributeEvent scab):若有属性对象加入Application的范围,通知正在收听的对象
attributeRemoved(ServletContextAttributeEvent scab):若有属性对象从Application的范围移除,通知正在收听的对象
attributeReplaced(ServletContextAttributeEvent scab):若在Application的范围中,有对象取代另一个对象时,通知正在收听的对象
监听会话范围内事件
管理从同一个客户端或用户向一个Web应用程序发出的一系列请求相关的状态或资源
HttpSessionBindingListener接口
HttpSessionAttributeListener接口
HttpSessionListener接口
HttpSessionActivationListener接口
HttpSessionBindingListener接口
监听对象加入Session范围时
监听从Session范围中移出对象时
接口有两个方法
void valueBound(HttpSessionBindingEvent event):当对象正在绑定到Session中,Servlet容器调用该方法来通知该对象
void valueUnbound(HttpSessionBindingEvent event):当从Session中删除对象时,Servlet容器调用该方法来通知该对象
HttpSessionBindingEvent类提供如下方法:
public String getName():返回绑定到Session中或从Session中删除的属性名字。
public Object getValue():返回被添加、删除、替换的属性值
public HttpSession getSession():返回HttpSession对象
HttpSessionAttributeListener
监听HttpSession中的属性的操作
当在Session中增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;
当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;
当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。
HttpSessionAttributeListener
监听HttpSession中的属性的操作
当在Session中增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;
当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;
当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。
HttpSessionListener接口
监听HttpSession对象的创建和销毁操作
当创建一个Session时,激发session Created(HttpSessionEvent se)方法
当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法
监听请求生命周期内事件
请求作用域范围内的生命周期事件用于管理整个request生命周期的状态和资源
ServletRequestListener接口
public void requestDestroyed(ServletRequestEvent sre):当请求被销毁时被处理。
public void requestInitialized(ServletRequestEvent sre):当请求被创建时被处理
ServletRequestAttributeListener接口
public void attributeAdded(ServletRequestAttributeEvent arg0) :当在请求作用域中添加一个属性的时候调用该方法。
public void attributeRemoved(ServletRequestAttributeEvent arg0) :当在请求作用域中删除一个属性时调用
public void attributeReplaced(ServletRequestAttributeEvent arg0) 当在请求作用域中替换一个属性值的时候调用
我们已经学到在servlet里可以用一个名字绑定一个对象,并且在应用中传递和使用这个对象。这个名字和对象所构成的名字/值被称为属性。这里所谓的属性就是一个对象。被设置(绑定)到另外三个对象:servletcontect、httpservletRequest、HttpSession。
作用域:谁能看到并使用这个属性,以及他能存活多久。
作用域对象 | 属性操作方法 | 作用域范围说明 |
ServletContext(上下文) | void setAttribute(String, Object) Object getAttribute(Sting) void removeAttribute(String) Enumeration getAttributeNames() | 整个Web应用程序 |
HttpSession(会话) | 一个会话交互过程 | |
ServletRequest(请求) | 一次请求过程 |
2.回话作用域
ServletContext应用上下文
设置在ServletContext对象中的属性可以被Web应用中的任何一部分来访问
3.请求作用域
系统的资源消耗
属性可以保存在请求作用域范围中
存储时间比上下文作用域和会话作用域短。在请求结束后,对象就会被垃圾回收
在请求作用域范围之外就不能进行访问
监听器概述
监听session,request,application这三个对象里存取数据的变化
监听器对象可以在事情发生前、发生后可以做一些必要的处理
Servlet监听器主要目的是给Web应用增加事件处理机制,以便更好地监视和控制Web应用的状态变化
监听器分类
事件类型 | 描述 | Listener接口 | |
ServletContext 事件 | 生命周期 | Servlet上下文刚被创建并可以开始为第一次请求服务,或者Servlet上下文将要被关闭发生的事件 | ServletContextListener |
属性改变 | Servlet上下文内的属性被增加、删除或者替换时发生的事件 | ServletContextAttributeListener | |
HttpSession 事件 | 生命周期 | HttpSession被创建、无效或超时时发生 | HttpSessionListener HttpSessionActivationListener |
会话迁移 | HttpSession被激活或钝化时发生 | ||
属性改变 | 在HttpSession中的属性被增加、删除、替换时发生 | HttpSessionAttributeListener HttpSessionBindingListener | |
对象绑定 | 对象被绑定到或者移出HttpSession时发生。 | ||
ServletRequest 事件 | 生命周期 | 在Servletr请求开始被Web组件处理时发生 |
Web应用启动和销毁事件
Web应用程序的属性发生改变的事件(包括增加、删除、修改)。
定义了ServletContextListener和ServletContextAttributeListener两个接口
ServletContextListener接口用于监听Web应用程序启动和销毁的事件
contextInitialized(ServletContextEvent sce):通知正在接受的对象,应用程序已经被加载及初始化
contextDestroyed(ServletContextEvent sce):通知正在接受的对象,应用程序已经被销毁
ServletContextAttributeListener接口
监听WEB应用属性改变的事件,包括:增加属性、删除属性、修改属性
attributeAdded(ServletContextAttributeEvent scab):若有属性对象加入Application的范围,通知正在收听的对象
attributeRemoved(ServletContextAttributeEvent scab):若有属性对象从Application的范围移除,通知正在收听的对象
attributeReplaced(ServletContextAttributeEvent scab):若在Application的范围中,有对象取代另一个对象时,通知正在收听的对象
监听会话范围内事件
管理从同一个客户端或用户向一个Web应用程序发出的一系列请求相关的状态或资源
HttpSessionBindingListener接口
HttpSessionAttributeListener接口
HttpSessionListener接口
HttpSessionActivationListener接口
HttpSessionBindingListener接口
监听对象加入Session范围时
监听从Session范围中移出对象时
接口有两个方法
void valueBound(HttpSessionBindingEvent event):当对象正在绑定到Session中,Servlet容器调用该方法来通知该对象
void valueUnbound(HttpSessionBindingEvent event):当从Session中删除对象时,Servlet容器调用该方法来通知该对象
HttpSessionBindingEvent类提供如下方法:
public String getName():返回绑定到Session中或从Session中删除的属性名字。
public Object getValue():返回被添加、删除、替换的属性值
public HttpSession getSession():返回HttpSession对象
HttpSessionAttributeListener
监听HttpSession中的属性的操作
当在Session中增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;
当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;
当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。
HttpSessionAttributeListener
监听HttpSession中的属性的操作
当在Session中增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;
当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;
当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。
HttpSessionListener接口
监听HttpSession对象的创建和销毁操作
当创建一个Session时,激发session Created(HttpSessionEvent se)方法
当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法
监听请求生命周期内事件
请求作用域范围内的生命周期事件用于管理整个request生命周期的状态和资源
ServletRequestListener接口
public void requestDestroyed(ServletRequestEvent sre):当请求被销毁时被处理。
public void requestInitialized(ServletRequestEvent sre):当请求被创建时被处理
ServletRequestAttributeListener接口
public void attributeAdded(ServletRequestAttributeEvent arg0) :当在请求作用域中添加一个属性的时候调用该方法。
public void attributeRemoved(ServletRequestAttributeEvent arg0) :当在请求作用域中删除一个属性时调用
public void attributeReplaced(ServletRequestAttributeEvent arg0) 当在请求作用域中替换一个属性值的时候调用
相关文章推荐
- 对象作用域与Servlet事件监听器
- 对象作用域与Servlet事件监听器
- 对象作用域与Servlet事件监听器
- 对象作用域与Servlet事件监听器
- 对象作用域与Servlet事件监听器
- 第六章 对象作用域与servlet事件监听器
- 对象作用域与Servlet事件监听器
- 对象作用域与Servlet事件监听器
- 对象作用域与servlet事件监听器
- 对象作用域与servlet事件监听器
- 对象作用域与Servlet事件监听器
- [原创]java WEB学习笔记48:其他的Servlet 监听器:域对象中属性的变更的事件监听器 (3 个),感知 Session 绑定的事件监听器(2个)
- Servlet事件监听器、监听servletContext域对象创建和销毁、监听三个域对象属性变化
- servlet事件监听器
- Servlet事件监听器
- Servlet事件监听器(一)
- WEB-Servlet事件监听器
- Servlet事件监听器
- Servlet的事件监听器
- 2011级-csdn-java-张侃— Servlet事件监听器