Javaweb Learning Summary (47)--The application of Listener (Listener) in development

Source: Internet
Author: User

listeners use more in javaweb development, and the following is a common application of the Listener (Listener) in developmentI. Statistics of the current number of people online

In Javaweb application development, sometimes we need to count the number of users currently online, so we can use listener technology to implement this feature.

 1 package Me.gacl.web.listener; 2 3 Import Javax.servlet.ServletContext; 4 Import javax.servlet.http.HttpSessionEvent; 5 Import Javax.servlet.http.HttpSessionListener; 6 7/** 8 * @ClassName: Onlinecountlistener 9 * @Description: Statistics current number of users online * @author: Aloof Wolf one * @date: 2014-9-10 pm 10:01:2 612 *13 * * * public class Onlinecountlistener implements Httpsessionlistener {@Override17 public void Sessi  oncreated (httpsessionevent se) {servletcontext context = Se.getsession (). Getservletcontext (); Onlinecount = (Integer) context.getattribute ("Onlinecount"), if (onlinecount==null) {Context.setat  Tribute ("Onlinecount", 1),}else{23 onlinecount++;24 context.setattribute ("Onlinecount",         Onlinecount);}26}27 @Override29 public void sessiondestroyed (httpsessionevent se) {30 ServletContext context = Se.getsession (). Getservletcontext (); Integer Onlinecount = (IntEger) Context.getattribute ("Onlinecount"), if (onlinecount==null) {Context.setattribute ("Onlinecoun         T ", 1);}else{35 onlinecount--;36 context.setattribute (" Onlinecount ", Onlinecount); 37 }38}39}
Second, custom session scanner

When a Web application creates a lot of sessions, in order to avoid the session taking up too much memory, we can choose to manually destroy these memory sessions, then it can also be achieved by using listener technology.

  1 package Me.gacl.web.listener;  2 3 Import java.util.Collections;  4 Import java.util.LinkedList;  5 Import Java.util.List;  6 Import Java.util.ListIterator;  7 Import Java.util.Timer;  8 Import Java.util.TimerTask; 9 Import javax.servlet.ServletContextEvent; Ten import Javax.servlet.ServletContextListener; Import javax.servlet.http.HttpSession; Import javax.servlet.http.HttpSessionEvent; Import Javax.servlet.http.HttpSessionListener; /** * @ClassName: Sessionscanerlistener * @Description: Custom session Scanner * @author: Lonely Wolf * @date: 2014-9-1   0 PM 10:16:42 * */public class Sessionscanerlistener implements Httpsessionlistener,servletcontextlistener {23     /** * @Field: List 26 * Defines a collection storage server created by HttpSession * LinkedList is not a thread-safe collection 28 * */** * Private list

This is the listener's two simple application scenarios.

Javaweb Learning Summary (47)--The application of Listener (Listener) in development

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.