CopyCode The Code is 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)
{}
}
I am so depressed. After more than two hours, I first implemented the httpsessionattributelistener interface method, but I forgot to write it when implementing the class, when I add a session, if I do not add it, I have been searching for a long time, no errors, and I have tested it many times, that is, the added listener is useless. I thought my method was wrong, I got a letter right several times, but I still didn't find it. Well, later I saw the previous example and suddenly I found out that I was missing something before I added it. Be careful with the Program .