A good memory is better than a bad writer. 39-javaweb in application (1)

Source: Internet
Author: User

A listener is an object that listens to and handles events or state changes that occur on other objects and takes action immediately when the object being monitored occurs.
The listener in Javaweb is a special class defined in the servlet specification that listens to the creation and destruction events of ServletContext, HttpSession, and ServletRequest domain objects in a Web application. and listening for events in which the properties in these domain objects have been modified.
1, the servlet Listener classification
Several types of listeners are defined in the servlet specification, which are used to listen for three domain objects of the event source Servletcontext,httpsession and ServletRequest;
For operations on these three domain objects, the listener is divided into three types:
1. Listen for event listeners that are created and destroyed by the domain object itself.
2. Listen for added and deleted event listeners for the properties in the domain object.
3. Listen for event listeners that are bound to the state of an object in the HttpSession domain.

2. Monitoring the creation and destruction of ServletContext domain objects
The Servletcontextlistener interface is used to listen for ServletContext object creation and destruction events. Classes that implement the Servletcontextlistener interface can listen to the creation and destruction of ServletContext objects.
The contextinitialized (Servletcontextevent SCE) method is called when the ServletContext object is created.
The contextdestroyed (Servletcontextevent SCE) method is called when the ServletContext object is destroyed.
3. Write the source code to monitor the creation and destruction of ServletContext domain objects
Implement Servletcontextlistener interface, monitor the creation and destruction of ServletContext objects

 PackageCom.servlet.listener;ImportJavax.servlet.ServletContextEvent;ImportJavax.servlet.ServletContextListener;/** * Simple implementation of the Servletcontextlistener interface, can be ServletContext object creation and destruction of these two actions to listen. * @author Fan Fangming * * Public  class Easyservletcontextlistenerimplements Servletcontextlistener {       @Override     Public void contextinitialized(Servletcontextevent SCE) {System.out.println ("-----------ServletContext object Creation"); }@Override     Public void contextdestroyed(Servletcontextevent SCE) {System.out.println ("-----------ServletContext object Destruction"); }}

4. Modify Web. xml


<listener>
<description>ServletContextListener监听器</description>
<!--实现了ServletContextListener接口的监听器类 -->
<listener-class>com.servlet.listener.EasyServletContextListener</listener-class>
</listener>

5. Operation Result
Start the Web middleware and see the console output:
——— –servletcontext Object creation

A good memory is better than a bad writer. Listeners in 39-javaweb applications (1)

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.