监听器学习:
## 作用:
监听request、session、application三个域对象的创建,销毁和数据的变更
## 使用:
1、创建一个普通java类实现指定的接口
监听request的创建和销毁:ServletRequestListener
requestInitialized(ServletRequestEvent sre)
requestDestroyed(ServletRequestEvent sre)
形参:
ServletRequestEvent可以获取当前监听到的request对象,对request对象的中的资源进行操作。
监听request作用域数据的变更:ServletRequestAttributeListener
attributeAdded(ServletRequestAttributeEvent srae)
attributeRemoved(ServletRequestAttributeEvent srae)
attributeReplaced(ServletRequestAttributeEvent srae)
形参:
ServletRequestAttributeEvent可以获取当前被监听到的request中的数据。
geName()返回监听到的数据的键和getValue()返回监听的到的数据的值。
监听session的创建和销毁:HttpSessionListener
sessionCreated(HttpSessionEvent se)
sessionDestroyed(HttpSessionEvent se)
形参:
获取当前被监听到的session对象
监听session的作用域数据的变更:
attributeAdded(HttpSessionBindingEvent event)
attributeRemoved(HttpSessionBindingEvent event)
attributeReplaced(HttpSessionBindingEvent event)
形参:
获取当前监听到的session中的数据 getName()返回数据的键名,getValue()返回数据的值
监听application对象的创建和销毁:ServletContextListener
contextInitialized(ServletContextEvent sce)
contextDestroyed(ServletContextEvent sce)
形参:
获取application对象
监听application对象的数据的变更:ServletContextAttributeListener
attributeAdded(ServletContextAttributeEvent event)
attributeRemoved(ServletContextAttributeEvent event)
attributeReplaced(ServletContextAttributeEvent event)
形参:获取当前被监听的数据 getName()返回数据的键名,getValue()返回数据的值
2、在项目中的web.xml中配置监听器,让监听器生效
<listener>
<listener-class>监听器类的包名和类名</listener-class>
</listener>
代码:
public class MyListener implements ServletRequestListener,ServletRequestAttributeListener,HttpSessionListener,HttpSessionAttributeListener,ServletContextListener,ServletContextAttributeListener{
//监听Request对象的创建和销毁
@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("request对象被销毁了");
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
ServletRequest req = sre.getServletRequest();
System.out.println("request对象被创建了");
}
//监听request对象作用域数据的增加、修改、删除
@Override
public void attributeAdded(ServletRequestAttributeEvent srae) {
System.out.println("request中增加了一条数据");
}
@Override
public void attributeRemoved(ServletRequestAttributeEvent srae) {
// TODO Auto-generated method stub
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent srae) {
// TODO Auto-generated method stub
}
//监听session的创建和销毁
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("session被创建了11111");
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("session被销毁了");
}
//监听session数据的增加、删除、修改
@Override
public void attributeAdded(HttpSessionBindingEvent event) {
System.out.println("session中增加了数据");
}
@Override
public void attributeRemoved(HttpSessionBindingEvent event) {
// TODO Auto-generated method stub
}
@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
// TODO Auto-generated method stub
}
//监听application对象的创建(初始化)和销毁
@Override
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
//监听application对象的数据的增加,删除,修改
@Override
public void attributeAdded(ServletContextAttributeEvent event) {
}
@Override
public void attributeRemoved(ServletContextAttributeEvent event) {
// TODO Auto-generated method stub
}
@Override
public void attributeReplaced(ServletContextAttributeEvent event) {
// TODO Auto-generated method stub
}
}