Copy Code code as follows:
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 ("Initialize * * * *");
}
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)
{}
}
Good depressed oh, two hours, I put the first Httpsessionattributelistener interface to achieve the method, but in the implementation of the class forgot to write it, I add in the session if not to add, I looked for a long time, and no mistakes, and tested many times, is to add when the monitor is not used, I think I wrote the wrong way, I have a letter of several times, or did not find. Oh, in the later, I looked at the previous example, suddenly found, I this seems to be a little something, just add to, a moment, OK. It's really careful to do the procedure.