Error in java session

  • 2020-05-07 20:14:24
  • OfStack


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 the ****"); 
} 
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, have more than two hours, I put the first HttpSessionAttributeListener method implements the interface, but forgot to write it, in the realization of the class when I was in to add Session if add do not go in, I find for a long time, and no error, and tested many times, is added when listening in useless, I thought I write the wrong way, I a letter to a few times, still can't find. Oh, later, I looked at the previous example, all of a sudden found, I seem to be missing something, just add, 1, ok. It's very careful to program.

Related articles: