Configure in Web. xml:
< Listener > < Listener-class >cn.edu.hbcf.common.listener.SessionAttributeListener</listener-class> </listener>
Then write an entity class:
PackageCn.edu.hbcf.common.listener;ImportJava.util.Map;ImportJava.util.concurrent.ConcurrentHashMap;Importjavax.servlet.http.HttpSession;ImportJavax.servlet.http.HttpSessionAttributeListener;Importjavax.servlet.http.HttpSessionBindingEvent;Importcn.edu.hbcf.common.constants.WebConstants;Importcn.edu.hbcf.privilege.pojo.BaseUsers; Public classSessionattributelistenerImplementsHttpsessionattributelistener { Public StaticMap<string, httpsession> sessionmap=NewConcurrenthashmap<string, httpsession>(); @Override Public voidattributeadded (httpsessionbindingevent arg0) {if(Arg0.getname (). Equals (Webconstants.current_user)) {//Enumeration session = Arg0.getsession (). Getservletcontext (). Getattributenames ();HttpSession session =arg0.getsession (); //ServletContext application = Session.getservletcontext ();Baseusers user =(baseusers) Session.getattribute (Webconstants.current_user); if(Sessionmap.containskey (User.getaccount ())) {//kick off the previous login Try{HttpSession Session2=Sessionmap.remove (User.getaccount ()); Session2.invalidate (); } Catch(Exception e) {}//Application.removeattribute (User.getaccount ());} sessionmap.put (User.getaccount (), session); //Application.setattribute (User.getaccount (), user);}} @Override Public voidattributeremoved (Httpsessionbindingevent arg0) {} @Override Public voidattributereplaced (httpsessionbindingevent se) {//TODO auto-generated Method Stub }}
Session User account authentication (one user login, kick out the previous user)