java_ee_sdk-5-javadocs\docs\api\index.html中的javax.servlet.http下有
ServletContextListener: session的创建和销毁
HttpSessionAttributeListener:session的attribute属性改变
以下示例参考drp项目
一、ServletContextListener示例:
1、java代码中:
public class InitListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent sce) {
}
public void contextInitialized(ServletContextEvent sce) {
System.out.println("InitListener.init()");
System.out.println("创建系统的BeanFactory...");
//将抽象工厂放到ServletContext中
BeanFactory beanFactory = BeanFactory.getInstance();
sce.getServletContext().setAttribute("beanFactory", beanFactory);
//加入常量
sce.getServletContext().setAttribute("add", Constants.ADD);
sce.getServletContext().setAttribute("del", Constants.DEL);
sce.getServletContext().setAttribute("modify", Constants.MODIFY);
sce.getServletContext().setAttribute("showAdd", Constants.SHOW_ADD);
sce.getServletContext().setAttribute("audit", Constants.AUDIT);
}
}
2、web.xml中:
<listener>
<listener-class>com.bjpowernode.drp.util.listener.InitListener</listener-class>
</listener>
二、HttpSessionAttributeListener示例:用来显示当前用户在线数,不过有半小时的误差,因为session关闭后tomcat服务器默认半小时后才会发现。
1、java代码中:
public class TestHttpSessionAttributeListener implements HttpSessionAttributeListener {
public void attributeAdded(HttpSessionBindingEvent se) {
System.out.println("TestHttpSessionAttributeListener-->>>attributeAdded()");
System.out.println("name=====" + se.getName());
System.out.println("value=====" + se.getValue());
if ("user_info".equals(se.getName())) {
Integer count = (Integer)se.getSession().getServletContext().getAttribute("count");
if (count == null) {
count = 1;
}else {
count++;
}
se.getSession().getServletContext().setAttribute("count", count);
}
}
public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("TestHttpSessionAttributeListener-->>>attributeRemoved()");
}
public void attributeReplaced(HttpSessionBindingEvent se) {
System.out.println("TestHttpSessionAttributeListener-->>>attributeReplaced()");
}
}
2、web.xml中:
<listener>
<listener-class>com.bjpowernode.drp.util.listener.TestHttpSessionAttributeListener</listener-class>
</listener>
ServletContextListener: session的创建和销毁
HttpSessionAttributeListener:session的attribute属性改变
以下示例参考drp项目
一、ServletContextListener示例:
1、java代码中:
public class InitListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent sce) {
}
public void contextInitialized(ServletContextEvent sce) {
System.out.println("InitListener.init()");
System.out.println("创建系统的BeanFactory...");
//将抽象工厂放到ServletContext中
BeanFactory beanFactory = BeanFactory.getInstance();
sce.getServletContext().setAttribute("beanFactory", beanFactory);
//加入常量
sce.getServletContext().setAttribute("add", Constants.ADD);
sce.getServletContext().setAttribute("del", Constants.DEL);
sce.getServletContext().setAttribute("modify", Constants.MODIFY);
sce.getServletContext().setAttribute("showAdd", Constants.SHOW_ADD);
sce.getServletContext().setAttribute("audit", Constants.AUDIT);
}
}
2、web.xml中:
<listener>
<listener-class>com.bjpowernode.drp.util.listener.InitListener</listener-class>
</listener>
二、HttpSessionAttributeListener示例:用来显示当前用户在线数,不过有半小时的误差,因为session关闭后tomcat服务器默认半小时后才会发现。
1、java代码中:
public class TestHttpSessionAttributeListener implements HttpSessionAttributeListener {
public void attributeAdded(HttpSessionBindingEvent se) {
System.out.println("TestHttpSessionAttributeListener-->>>attributeAdded()");
System.out.println("name=====" + se.getName());
System.out.println("value=====" + se.getValue());
if ("user_info".equals(se.getName())) {
Integer count = (Integer)se.getSession().getServletContext().getAttribute("count");
if (count == null) {
count = 1;
}else {
count++;
}
se.getSession().getServletContext().setAttribute("count", count);
}
}
public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("TestHttpSessionAttributeListener-->>>attributeRemoved()");
}
public void attributeReplaced(HttpSessionBindingEvent se) {
System.out.println("TestHttpSessionAttributeListener-->>>attributeReplaced()");
}
}
2、web.xml中:
<listener>
<listener-class>com.bjpowernode.drp.util.listener.TestHttpSessionAttributeListener</listener-class>
</listener>