Copy codeThe 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)
{}
}
So depressing. 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 very careful with the program.