Listener执行流程(二)

2014-11-24 07:11:53 · 作者: · 浏览: 1
("user_name".equals(se.getName())) {
Integer count = (Integer)se.getSession().getServletContext().getAttribute("count");
if (count == null) {
count = 1;
}else {
count++;
}
se.getSession().getServletContext().setAttribute("count", count);
}
System.out.println("count=" + se.getSession().getServletContext().getAttribute("count"));
}

@Override
public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("TestHttpSessionAttributeListener===>>>>>attributeRemoved" + se.getSession().getServletContext().getAttribute("count"));
}

@Override
public void attributeReplaced(HttpSessionBindingEvent arg0) {
System.out.println("TestHttpSessionAttributeListener===>>>>>attributeReplaced");
}

}
package listener;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;


public class TestHttpSessionAttributeListener implements
HttpSessionAttributeListener {

@Override
public void attributeAdded(HttpSessionBindingEvent se) {
System.out.println("name==========" + se.getName());
System.out.println("value==========" + se.getValue());
if ("user_name".equals(se.getName())) {
Integer count = (Integer)se.getSession().getServletContext().getAttribute("count");
if (count == null) {
count = 1;
}else {
count++;
}
se.getSession().getServletContext().setAttribute("count", count);
}
System.out.println("count=" + se.getSession().getServletContext().getAttribute("count"));
}

@Override
public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("TestHttpSessionAttributeListener===>>>>>attributeRemoved" + se.getSession().getServletContext().getAttribute("count"));
}

@Override
public void attributeReplaced(HttpSessionBindingEvent arg0) {
System.out.println("TestHttpSessionAttributeListener===>>>>>attributeReplaced");
}

}

代码写完后,还需要在Web.xml中进行一下配置:

[html] < xml version="1.0" encoding="UTF-8" >
http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">


listener.TestHttpSessionListener



listener.TestHttpSessionAttributeListener



< xml version="1.0" encoding="UTF-8" >
http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">


listener.TestHttpSessionListener



listener.TestHttpSessionAttributeListener

执行结果:

代码解析:

1.实现接口

本例是实现了两个监听,一个是对Session的监听(可以没有),一个是对SessionAttribute的监听,在这里将两者都写出来,是让大家对这两者进行一下区分(明白Session和SessionAttribute是分开进行监听的)。

既然是分开的,所以要实现不同的接口,至于为什么一定要实现接口,就是因为Web服务器对不同的接口已经进行了不同的封装,所以当你进行不同的操作时才会调用到不同的监听器上。

2.attributeAdded(HttpSessionBindingEvent se)

HttpSessionAttributeListener接口中的每一个方法中都有一个HttpSessionBindingEvent参数,它是一个事件对象,就是因为对于Session的Attribute的操作触发了该事件,所以才能将对session的attribute的操作触发到响应的方法来。

三.常用监听器

前面只是提到了两种监听器,大家知道不同的操作对应不同的监听器,下面我就来为大家介绍一些常用的监听器:

1.ServletContextAttributeListener:监听对 ServletContext 属性的操作,比如 增加,删除,修改属性.

2.ServletContextListener监听 ServletContext.

当创建 ServletContext 时,激 发 contextInitialized(ServletContextEventsce)方法;当销毁 ServletContext 时,激发 contextDestroyed(ServletContextEvent sce)方 法.

3.HttpSessionListener监听 HttpSession 的操作.

当创建一个 Session 时,激发 session Created(HttpSessionEvent se)方法;当销毁一个Session 时,激 发 s