【 tulaoshi.com - Web开发 】
代码如下:
package cn.lang.any.listener;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AmpList implements
ServletContextListener,HttpSessionListener,HttpSessionAttributeListener
{
private ServletContext application=null;
public void contextInitialized(ServletContextEvent sce)
{
this.application=sce.getServletContext();
this.application.setAttribute("alluser",new ArrayList());
//System.out.println("初始化****");
}
public void sessionCreated(HttpSessionEvent se)
{
//System.out.println("session ****");
}
public void sessionDestroyed(HttpSessionEvent se)
{
List l=(List)this.application.getAttribute("alluser");
String value=(String)se.getSession().getAttribute("uname");
l.remove(value);
this.application.setAttribute("alluser",l);
}
public void attributeAdded(HttpSessionBindingEvent se)
{
//System.out.println("Hello");
List lis=(List)this.application.getAttribute("alluser");
lis.add(se.getValue());
this.application.setAttribute("alluser",lis);
}
public void attributeRemoved(HttpSessionBindingEvent se)
{
}
public void attributeReplaced(HttpSessionBindingEvent se)
{
}
public void contextDestroyed(ServletContextEvent sce)
{}
}
好郁闷哦,搞了两个多小时,我把先把HttpSessionAttributeListener 接口的方法实现了,但是在类的实现时忘记写它了,我在Session进行添加时要是添加不进去,我找了好久,又没错误,又测试了很多遍,就是添加时的监听没有用,我以为我方法写错了,我一个字母的对了几遍,还是没发现。呵呵,在后来,我又看了前面的例子,突然之间就发现了,我这好像少了点什么,才加上去,一下了,好了。做程序真得细心呀。