监听器
在Jsp 2.0中,一共提供了8个监听器,详细列表如下:
监听器名称 |
作用 |
ServletContextListener |
负责监听服务器的启动和停止,需要重写contextInitialized(服务器的启动)和contextDestroyed()方法。 |
ServletContextAttributeListener |
负责监听ServletContext属性的添加、删除和替换。需要重写attributeAdded(添加)、attributeRemoved(删除)和attributeReplaced(替换)方法。 |
HttpSessionListener |
监听会话的创建和销毁。需要重写sessionCreated(创建)和sessionDestroyed(销毁)方法。 |
HttpSessionAttributeListener |
监听Session属性的添加、删除和替换。需要重写attributeAdded(添加)、attributeRemoved(删除)和attributeReplaced(替换)方法。 |
HttpSessionBindingListener |
这个监听器和其他的监听器有所不同。它要完成的功能是这样的:我们可以让一个类实现HttpSessionBindingListener接口,当我们将一个实现HttpSessionBindingListener接口的对象加入到Session中的时候,就会触发这个对象的valueBound方法,当我们从Session中删除这个对象的时候,就会触发这个对象的valueUnbound方法。这是一个不需要在web.xml中设定的监听器。 |
HttpSessionActivationListener |
这个监听器也是和HttpSessionBindingListener一样。当一个类实现HttpSessionActivationListener之后,如果将该对象加入到Session中,那么当Session钝化和激活的时候,它可以得到通知。当同一个Session在不同的JVM之间移动的时候,可以实现Session转台跟踪。需要重写sessionWillPassivate和sessionDidActivate方法。Objects that are bound to a session may listen to container events notifying them that sessions will be passivated and that session will be activated. A container that migrates session between VMs or persists sessions is required to notify all attributes bound to sessions implementing HttpSessionActivationListener。 |
ServletRequestListener |
这个是在Jsp 2.0中新添加的监听器。监听请求的产生和消灭。需要重写requestInitialized方法和requestDestroyed方法。通过这个listener主要是要获取request对象,从而可以得到一些request的信息。 |
ServletRequestActtributeListener |
这个监听器也是在Jsp 2.0中定义的。它是用来监听request范围内的变量的添加、修改和删除。需要重写attributeAdded、attributeReplaced和attributeRemoved方法。 |