现在的位置: 首页 > 综合 > 正文

servlet监听器

2014年08月29日 ⁄ 综合 ⁄ 共 6523字 ⁄ 字号 评论关闭

servlet 监听器

Listener接口

Event

ServletContextListener

ServletContextEvent

ServletContextAttributeListener

ServletContextAttributeEvent

HttpSessionListener

HttpSessionEvent

HttpSessionActivationListener

HttpSessionAttributeListener

HttpSessionBindingEvent

HttpSessionBindingListener

ServletRequestListener

ServletRequestEvent

ServletRequestAttributeListener

ServletRequestAttributeEvent

 

HttpSessionAttributeListener与HttpSessionBindingListener的主要区别是:

HttpSessionAttributeListener——用于监听session中何时添加、删除或替换了某种类型的属性;

HttpSessionBindingListener——由属性自身来实现,以便属性知道它什么时候被添加到一个session中,或者什么时候从session中删除

Servle监听器

在Servlet规范中定义了多种类型的监听器,它们用于监听的事件源分别为 ServletContext, HttpSession 和 ServletRequest 这三个域对象。

Servlet规范针对这三个对象上的操作,又把这多种类型的监听器划分为三种类型。
监听三个域对象创建和销毁的事件监听器
监听域对象中属性的增加和删除的事件监听器
监听绑定到 HttpSession 域中的某个对象的状态的事件监听器

监听servletContext域对象创建和销毁

ServletContextListener 接口用于监听 ServletContext 对象的创建和销毁事件。
当 ServletContext 对象被创建时,激发contextInitialized (ServletContextEvent sce)方法
当 ServletContext 对象被销毁时,激发contextDestroyed(ServletContextEvent sce)方法。

提问,servletContext域对象何时创建和销毁:
创建:服务器启动针对每一个web应用创建servletcontext
销毁:服务器关闭前先关闭代表每一个web应用的servletContext

编写 Servlet 监听器

和编写其它事件监听器一样,编写servlet监听器也需要实现一个特定的接口,并针对相应动作覆盖接口中的相应方法。
和其它事件监听器略有不同的是,servlet监听器的注册不是直接注册在事件源上,而是由WEB容器负责注册,开发人员只需在web.xml文件中使用<listener>标签配置好监听器,web容器就会自动把监听器注册到事件源中。
一个 web.xml 文件中可以配置多个 Servlet 事件监听器,web 服务器按照它们在 web.xml 文件中的注册顺序来加载和注册这些 Serlvet 事件监听器

监听HttpSession域对象创建和销毁

HttpSessionListener接口用于监听HttpSession创建和销毁
创建一个Session时,sessionCreated(HttpSessionEvent se) 方法将会被调用。
销毁一个Session时,sessionDestroyed (HttpSessionEvent se) 方法将会被调用。
Session域对象创建和销毁的时机创建:用户每一次访问时,服务器创建session
销毁:如果用户的session 30分钟没有使用,服务器就会销毁session,我们在web.xml里面也可以配置session失效时间
ServletRequestListener 接口用于监听ServletRequest 对象的创建和销毁。
Request 对象被创建时,监听器的requestInitialized方法将会被调用。
Request对象被销毁时,监听器的requestDestroyed方法将会被调用。
(此处复习request对象,在浏览器窗口中多次刷新访问servlet,看request对象的创建和销毁,并写一个servlet,然后用sendRedirect、forward方式跳转到其它servlet,查看request对象的创建和消耗)
servletRequest域对象创建和销毁的时机:
创建:用户每一次访问,都会创建一个reqeust
销毁:当前访问结束,request对象就会销毁

---------------------------------------------------------------------------------------------------

监听三个域对象属性变化

Servlet规范定义了监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信息事件的监听器。
这三个监听器接口分别是ServletContextAttributeListener, HttpSessionAttributeListener ServletRequestAttributeListener
这三个接口中都定义了三个方法来处理被监听对象中的属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同。

attributeRemoved 方法

 

当删除被监听对象中的一个属性时,web 容器调用事件监听器的这个方法进行相应
各个域属性监听器中的完整语法定义为:
public void attributeRemoved(ServletContextAttributeEvent scae) 
public void attributeRemoved (HttpSessionBindingEvent  hsbe) 
public void attributeRemoved (ServletRequestAttributeEvent srae)



attributeAdded 方法

当向被监听器对象中增加一个属性时,web容器就调用事件监听器的 attributeAdded 方法进行相应,这个方法接受一个事件类型的参数,监听器可以通过这个参数来获得正在增加属性的域对象和被保存到域中的属性对象

各个域属性监听器中的完整语法定义为:
public void attributeAdded(ServletContextAttributeEvent scae) 
public void attributeAdded(HttpSessionBindingEvent  hsbe) 
public void attributeAdded(ServletRequestAttributeEvent srae)

attributeReplaced 方法

当监听器的域对象中的某个属性被替换时,web容器调用事件监听器的这个方法进行相应
各个域属性监听器中的完整语法定义为:
public void attributeReplaced(ServletContextAttributeEvent scae) 
public void attributeReplaced (HttpSessionBindingEvent  hsbe) 
public void attributeReplaced (ServletRequestAttributeEvent srae)

------------------------------------------------------------------------------

Servlet监听器用于监听一些重要事件的发生,监听器对象可以在事情发生前、发生后可以做一些必要的处理。
接口:
目前Servlet2.4和JSP2.0总共有8个监听器接口和6个Event类,其中HttpSessionAttributeListener与
HttpSessionBindingListener皆使用HttpSessionBindingEvent;HttpSessionListener和HttpSessionActivationListener则都使用HttpSessionEvent;其余Listener对应的Event如下所示:
 
Listener接口
Event类
ServletContextListener
ServletContextEvent
ServletContextAttributeListener
ServletContextAttributeEvent
HttpSessionListener
HttpSessionEvent
HttpSessionActivationListener
HttpSessionAttributeListener
HttpSessionBindingEvent
HttpSessionBindingListener
ServletRequestListener
ServletRequestEvent
ServletRequestAttributeListener
ServletRequestAttributeEvent
 
分别介绍:
一 ServletContext相关监听接口
补充知识:
通过ServletContext 的实例可以存取应用程序的全局对象以及初始化阶段的变量。
在JSP文件中,application 是 ServletContext 的实例,由JSP容器默认创建。Servlet 中调用 getServletContext()方法得到 ServletContext 的实例。
注意:
全局对象即Application范围对象,初始化阶段的变量指在web.xml中,经由<context-param>元素所设定的变量,它的范围也是Application范围,例如:
<context-param>
<param-name>Name</param-name>
<param-value>browser</param-value>
</context-param>
当容器启动时,会建立一个Application范围的对象,若要在JSP网页中取得此变量时:
String name = (String)application.getInitParameter("Name");
或者使用EL时:
${initPara.name}
若是在Servlet中,取得Name的值方法:
String name = (String)ServletContext.getInitParameter("Name");

1.ServletContextListener:
用于监听WEB 应用启动和销毁的事件,监听器类需要实现javax.servlet.ServletContextListener 接口。
ServletContextListener 是 ServletContext 的监听者,如果 ServletContext 发生变化,如服务器启动时 ServletContext 被创建,服务器关闭时 ServletContext 将要被销毁。
ServletContextListener接口的方法:
void contextInitialized(ServletContextEvent sce)
通知正在接受的对象,应用程序已经被加载及初始化。
void contextDestroyed(ServletContextEvent sce)
通知正在接受的对象,应用程序已经被载出。
ServletContextEvent中的方法:
ServletContext getServletContext()
取得ServletContext对象

2.ServletContextAttributeListener:用于监听WEB应用属性改变的事件,包括:增加属性、删除属性、修改属性,监听器类需要实现javax.servlet.ServletContextAttributeListener接口。
ServletContextAttributeListener接口方法:
void attributeAdded(ServletContextAttributeEvent scab)
若有对象加入Application的范围,通知正在收听的对象
void attributeRemoved(ServletContextAttributeEvent scab)
若有对象从Application的范围移除,通知正在收听的对象
void attributeReplaced(ServletContextAttributeEvent scab)
若在Application的范围中,有对象取代另一个对象时,通知正在收听的对象

ServletContextAttributeEvent中的方法:
java.lang.String getName()
回传属性的名称
java.lang.Object getValue()
回传属性的值
二、HttpSession相关监听接口
1.HttpSessionBindingListener接口
注意:HttpSessionBindingListener接口是唯一不需要再web.xml中设定的Listener
当我们的类实现了HttpSessionBindingListener接口后,只要对象加入Session范围(即调用HttpSession对象的setAttribute方法的时候)或从Session范围中移出(即调用HttpSession对象的removeAttribute方法的时候或Session Time out的时候)时,容器分别会自动调用下列两个方法:
void valueBound(HttpSessionBindingEvent event)
void valueUnbound(HttpSessionBindingEvent event)
思考:如何实现记录网站的客户登录日志, 统计在线人数?
2.HttpSessionAttributeListener接口
HttpSessionAttributeListener监听HttpSession中的属性的操作。
当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。这和ServletContextAttributeListener比较类似。
3.HttpSessionListener接口
HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
4.HttpSessionActivationListener接口
主要用于同一个Session转移至不同的JVM的情形。
四、ServletRequest监听接口
1.ServletRequestListener接口
和ServletContextListener接口类似的,这里由ServletContext改为ServletRequest
2.ServletRequestAttributeListener接口
和ServletContextListener接口类似的,这里由ServletContext改为ServletRequest

抱歉!评论已关闭.