JSP Note--9.listener Introduction

Source: Internet
Author: User
Tags event listener

Listener can be used to listen to different Web events. With listener, you first have to have a listener implementation class, and then you configure the listener (or through annotations) in Web. Xml. The common Web Event listener interface is as follows:

    • Servletcontextlistener: Listening for web App startup and shutdown

    • Servletcontextlistener: Monitoring ServletContext Range (application) changes in content properties

    • Servletrequestlistener: Listening for user requests

    • Servletrequestattributelistener: Listening for attribute changes in the ServletRequest range

    • Httpsessionlistener: For listening to the start and end of a session

    • Httpsessionattributelistener: Monitoring the changes in the session properties

The methods in the Servletcontextlistener interface, contestinitialized () and contextdestroyed () methods are triggered when the application is started and closed.


Configure Listener

Configuration listener is very simple, just need to simply drop the specified listener implementation class on the line, cannot configure initialization parameters. Simply configure the listener implementation class with the listener-class of the listener child element in XML. If you configure a listener class that implements Servletcontextlistener, the listener contextinitialized method is triggered when the Web application is started.


Using Servletcontextattributelistener

This interface can be used to listen for changes in the application range of properties. The implementation of this interface requires the following three methods:

Attributeadded (Servletcontextattributeevent Event): Deposit attribute Trigger

Attributeremoved (servletcontentattributeevent event): Delete attribute trigger

attributereplaced(servletcontentattributeevent event): Replace property trigger


Using Servletrequestlistener and Servletrequestattributelistener

The Servletrequestlistener is used to listen for user requests arriving. Implementing this interface requires both requestinitialized (Servletrequestevent SRE) and requestdestroyed (Servletrequestevent SRE) interfaces. The first one is triggered when a user request arrives, is initialized, and the second is triggered when the user requests are terminated and destroyed.

ServletRequest The Attributelistener is used to listen for changes within the request range and requires the implementation of the attributeadded (), attributeremoved (), attributereplaced () three methods

If a listener class implements multiple interfaces, it can listen for application range changes and listen for the request range changes.

Httpsessionlistener and Httpsessionattributelistener

Httpsessionlistener is used to monitor the creation and destruction of sessions, which requires implementation of sessioncreated (Httpsessionevent SE): Methods and Sessiondestroyed ( Httpsessionevent se).

Similarly, Httpsessionattributelistener is used to monitor changes in the Httpsesson range, which requires the implementation of the attributeadded, attributeremoved, and attributereplaced three methods. The listener that implements the Httpsessonlistener interface listens to the start and disconnect of each user's reply, so that the interface can be used to listen to the users of the system online. In addition, by analyzing the request, we can get the information of the user online in more detail.

This article from "Fingertip Light Fly" blog, declined reprint!

JSP Note--9.listener Introduction

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.