Servlet监听器详解

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
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值