復制代碼 代碼如下:
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進行添加時要是添加不進去,我找了好久,又沒錯誤,又測試了很多遍,就是添加時的監聽沒有用,我以為我方法寫錯了,我一個字母的對了幾遍,還是沒發現。呵呵,在後來,我又看了前面的例子,突然之間就發現了,我這好像少了點什麼,才加上去,一下了,好了。做程序真得細心呀。