在Servlet里可以用一个名字绑定一个对象
谁能看到并使用这个属性,它能存活多久
作用域对象 | 属性操作方法 | 作用域范围说明 |
ServletContext(上下文) | void setAttribute(String, Object) Object getAttribute(Sting) void removeAttribute(String) Enumeration getAttributeNames() | 整个Web应用程序 |
HttpSession(会话) | 一个会话交互过程 | |
ServletRequest(请求) |
ServletContext应用上下文
设置在ServletContext对象中的属性可以被Web应用中的任何一部分来访问
会话作用域
Session用于维护与一个客户的会话状态。对于同一个客户的多个请求,Session会跨这些请求持久存储
请求作用域
系统的资源消耗
属性可以保存在请求作用域范围中
监听器概述
监听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应用启动和销毁事件
Web应用程序的属性发生改变的事件(包括增加、删除、修改)。
定义了ServletContextListener和ServletContextAttributeListener两个接口
ServletContextListener接口
ServletContextListener接口用于监听Web应用程序启动和销毁的事件
contextInitialized(ServletContextEvent sce):通知正在接受的对象,应用程序已经被加载及初始化
contextDestroyed(ServletContextEvent sce):通知正在接受的对象,应用程序已经被销毁
<listener>
<listener-class>
com.lovobook.MyServletContextListener
</listener-class>
</listener>
public class MyServletContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {}
public void 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) 方法。
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中3个对象作用域分别由ServletContext、ServletRequest和HttpSession接口来处理
上下文作用域中设置的属性是线程不安全的
对于同一个客户的多个请求,Session会跨这些请求持久存储
设置在上下文和会话作用域中的对象,会非常消耗系统的资源
请求作用域范围仅仅作用在与一个请求相关的两个资源之间
应用程序事件监听器是实现一到多个Servlet事件监听器接口的类。它们在Web应用程序部署的时候,被Web容器初始化和注册
ServletContext监听器用于管理应用程序JVM级别保存的资源或状态。
HTTP会话监听器用于管理从同一客户端或用户发送的一系列请求的资源或状态。
Request请求监听器用于管理Request请求生命周期内的状态