1 Package Sanglp.servlet;2 3 4 import Javax.servlet.ServletContext;5 import Javax.servlet.annotation.WebListener;6 import javax.servlet.http.HttpSession;7 import javax.servlet.http.HttpSessionEvent;8 import java.util.Hashtable;9 import Java.util.Map;Ten One /** A * Created by Administrator on 2016/10/6. - */ - @WebListener the Public classHttpsessionlistener implements Javax.servlet.http.HttpSessionListener { - - //This method is triggered when a session is started between the user and the service - @Override + Public voidsessioncreated (httpsessionevent httpsessionevent) { -HttpSession session=httpsessionevent.getsession (); +ServletContext application=Session.getservletcontext (); A //Get SessionID atString sessionid=Session.getid (); - //If this is a new session - if(Session.isnew ()) { -String user= (String) Session.getattribute ("User"); - //user not logged in as guest handling -User= (user==NULL)?"Visitors": User; inMap<string,string> online= (map<string, string>) Application.getattribute ("Online"); - if(online==NULL){ toOnline=NewHashtable<string,string>(); + //put the user's online information in a map - Online.put (sessionid,user); theApplication.setattribute ("Online", online); * } $ }Panax Notoginseng } - the //This method is triggered when the session disconnects between the user and the server + @Override A Public voidsessiondestroyed (httpsessionevent httpsessionevent) { theHttpSession session=httpsessionevent.getsession (); +ServletContext application=Session.getservletcontext (); -String sessionid=Session.getid (); $Map<string,string> online= (map<string,string>) Application.getattribute ("Online"); $ if(online!=NULL){ - //Delete the user's online information - Online.remove (sessionId); the } -Application.setattribute ("Online", online);Wuyi } the}
"Java EE Enterprise Application Combat Learning record" Sessionlistener