"Java EE Enterprise Application Combat Learning record" Sessionlistener

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.