Java-listener, java listener example

Source: Internet
Author: User

Java-listener, java listener example

A Listener is also called a Listener. It is a Servlet Listener that can listen to client requests and server operations. Through the listener, some operations can be automatically triggered, such as the number of online users.

For example, ServletContextListener listens to the ServletContext object.

Create a web Project

Create CountUtils and ListenerTest classes and write code

Configure web. xml

Compile a jsp page

Run the code and access it (two people access it at the same time)

 

Common listening interfaces:

ServletContextAttributeListener listens for operations on the attributes of ServletContext, such as adding, deleting, and modifying attributes.

ServletContextListener listens to the ServletContext object.

When a ServletContext is created, the contextInitialized (ServletContextEvent sce) method is activated;

When ServletContext is destroyed, the contextDestroyed (ServletContextEvent sce) method is triggered.

 

HttpSessionListener:

When a Session is created, the sessionCreated (HttpSessionEvent se) method is activated;

When a Session is destroyed, the sessionDestroyed (HttpSessionEvent se) method is triggered.

HttpSessionAttributeListener:

When an attribute is added to a Session, the attributeAdded (HttpSessionBindingEvent se) method is triggered;

When a Session deletes an attribute, the attributeRemoved (HttpSessionBindingEvent se) method is triggered;

When the Session attribute is reset, The attributeReplaced (HttpSessionBindingEvent se) method is triggered.

 

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.