您的位置:首页 > Web前端 > JavaScript

jsp监听器listener

2016-07-24 11:28 513 查看
监听器:要知道各种类型【监听器】【触发】的【时机】,了解后,在【合适】的【时机】【使用】【合适】的【监听器】;

web.xml中配置监听器:监听器时web容器在合适的时机调用的,所有不需要像servlet、filter显示的设置name、url等其他信息,

只需要项目启动后,把监听器加载在web容器中即可,加载方式如下,配置监听:

<listener>

      <listener-class>

          <!-- 监听器全路径 -->

          com.listener.WebContextInitListener

      </listener-class>

</listener>

HttpSessionBindingListener: 不需要再web.xml中配置

实现该接口的类(例如:User),

创建的对象(例如:user)被添加到session中(session.setAttribute(key, user)),

这时,就触发了HttpSessionBindingEvent【绑定】事件,从而调用User类中的valueBound()方法,

如果调用session.removeAttribute(key),

这只,就触发了HttpSessionBindingEvent【解除绑定】事件,从而调用User类中的valueUnBound()方法;

*session.setAttribute,这是调用valueBound的时机,session.removeAttribute,这是调用valueUnBound的时机;

HttpSessionListener:需要在web.xml中配置

Listener

Listener是Servlet的监听器

监听客户端的请求和服务器端的操作

通过实现Listener接口的类可以在特定事件(Event)发生时,自动激发一些操作

HttpSessionBindingListener

当一个实现了该接口的对象被捆绑到session中或从session中被解放的时候启用此监听

关键点

创建类实现HttpSessionBindingListener接口

valueBound()

valueUnbound()

不需要在web.xml中配置监听器

监听范围:一对一

HttpSessionListener

在WEB应用中,当一个session被创建或者销毁时启用这个监听器

关键点

sessionCreated(HttpSessionEvent event)

客户端第一次和服务器交互时候触发

sessionDestroyed(HttpSessionEvent event)

销毁会话的时候触发

必须在web.xml中配置监听器

监听范围:设置一次就可以监听所有session

ServletContextListener

在WEB应用中,当服务启动或者停止时启用这个监听器

关键点

contextInitialized(ServletContextEvent arg)

WEB服务启动的时候触发

contextDestroyed(ServletContextEvent arg)WEB

WEB服务停止的时候触发

必须在web.xml中配置监听器

监听范围:监听web应用的生命周期

HttpSessionAttributeListener

监听HttpSession(session)范围内属性的变化

关键点:

attributeAdded(HttpSessionBindingEvent arg0)

在session中添加对象时触发此操作(setAttribute)

attributeRemoved(HttpSessionBindingEvent arg0)

修改、删除session中对象时触发此操作(removeAttribute)

attributeReplaced(HttpSessionBindingEvent arg0)

在session属性被重新设置时
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jsp listener 监听器