Java Web event monitoring

Source: Internet
Author: User

1. Listener Introduction

A listener is a common Java program that implements a specific interface. This program is used to listen for method calls or attribute changes of another Java object. After the above events occur on the Monitored object, A Method of the listener will be executed immediately.

 

2. servlet listener

Multiple types of listeners are defined in the servlet specification. The event sources used for listening are servlecontext, httpsession, and servletrequest domain objects.

Servlet APIs provide eight listener interfaces:

● Servletcontextlistener: The application context Life Cycle listener. It is used to listen to the creation and destruction of the servletcontext object.

● Servletcontextattributelistener: Listener of the application context attribute event. Event used to listen for attribute changes in the Web application context.

● Servletrequestlistener: Request lifecycle listener. Used to listen for request creation and destruction events.

● Servletrequestattributelistener: Request attribute event listener. Event used to listen for attribute changes in the request.

● Httpsessionlistener: The session lifecycle listener. The time when the session is created and destroyed.

● Httpsessionactivationlistenerlistener: Session activation and passivation event listener. It is used to listen for sessions activation and deactivation events.

● Httpsessionattributelistener: Session property event listener. Used to listen for events in which attributes change in a session.

● Httpsessionbindinglistener: Session binding event listener. This is the only listener that does not need to be set in Web. xml.

Some callback methods are defined in each listener interface. When a corresponding event occurs or occurs, the Web Container automatically calls the corresponding methods in the listener implementation class.

Multiple types of listeners are defined in the servlet specification. The event sources used for listening are servletcontext, httpsession, and servletrequest domain objects.

3. Listen to the creation and destruction of servletcontext domain objects

The servletcontextlistener interface is used to listen to the creation and destruction events of the servletcontext object.
The following is an example:
Compile a myservletcontextlistener class to implement the servletcontextlistener interface. The specific code is as follows:
Public class myservletcontextlistener implements servletcontextlistener {
 
Public void contextdestroyed (servletcontextevent arg0 ){
System. Out. println (arg0.getsource () + "destroyed ");

}

Public void contextinitialized (servletcontextevent arg0 ){
System. Out. println (arg0.getsource () + "created ");

}
}

Then register the listener in Web. xml.

<Listener>
<Listener-class> CN. csdn. Web. listener. myservletcontextlistener </listener-class>
</Listener>

2. Listen to httpsession domain object creation and destruction

The httpsessionlistener interface is used to listen to the creation and destruction of httpsession.
When you create a session, the sessioncreated (httpsessionevent SE) method will be called.
When a session is destroyed, the sessiondestroyed (httpsessionevent SE) method will be called.
The following is an example:
Compile a myhttpsessionlistener class to implement the httpsessionlistener interface. The specific code is as follows:

Public class myhttpsessionlistener implements httpsessionlistener {

Public void sessioncreated (httpsessionevent arg0 ){
System. Out. println ("Session Object" + arg0.getsession () + "created ");

}

Public void sessiondestroyed (httpsessionevent arg0 ){
System. Out. println ("Session Object" + arg0.getsession () + "destroyed ");
}

}

Then register the listener in Web. xml.

<Listener>
<Listener-class> CN. csdn. Web. listener. myhttpsessionlistener </listener-class>
</Listener>

3. Listen to the httprequest domain object creation and destruction

The servletrequestlistener interface is used to listen to the creation and destruction of the servletrequest object.
When a request object is created, the listener's requestinitialized method will be called.
When the request object is destroyed, the listener's requestdestroyed method will be called.

The following is an example:
Compile a myservletrequestlistener class to implement the servletrequestlistener interface. The specific code is as follows:
Public void requestdestroyed (servletrequestevent arg0 ){
System. Out. println ("request object" + arg0.getservletrequest () + "created ");

}

Public void requestinitialized (servletrequestevent arg0 ){
System. Out. println ("request object" + arg0.getservletrequest () + "destroyed ");

}

}

Then register the listener in Web. xml.

<Listener>
<Listener-class> CN. csdn. Web. listener. myservletrequestlistener </listener-class>
</Listener>

 

Today, we introduced the creation and destruction of the servletcontext, httpsession, and servletrequest domain objects.

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.