Bad memory:-listeners in Java Web Applications (1) and-Java Web listeners

Source: Internet
Author: User

Bad memory:-listeners in Java Web Applications (1) and-Java Web listeners

A listener is an object that is used to listen for and process events or state changes on other objects. When the Monitored Objects occur, they take corresponding actions immediately.
The listener in Java web is a special class defined in the Servlet specification. It is used to listen to the creation and destruction events of domain objects such as ServletContext, HttpSession, and ServletRequest in web applications, and events that listen to the modification of attributes in these domain objects.
1. Servlet listener Classification
Multiple types of listeners are defined in the Servlet specification. The event sources used for listening are ServletContext, HttpSession, and ServletRequest domain objects;
For the operations on these three domain objects, the listener is divided into three types:
1. event listeners that listen to the creation and destruction of domain objects.
2. Listen to added and deleted event listeners in the attributes of the domain object.
3. Listen to the event listener bound to the status of an object in the HttpSession domain.

2. Listen to the creation and destruction of the ServletContext domain object
The ServletContextListener interface is used to listen to the creation and destruction events of the ServletContext object. Classes that implement the ServletContextListener interface can listen to the creation and destruction of the ServletContext object.
When the ServletContext object is created, the contextInitialized (ServletContextEvent sce) method is called.
When the ServletContext object is destroyed, the contextDestroyed (ServletContextEvent sce) method is called.
3. Compile the source code that listens to the creation and destruction of the ServletContext domain object.
Implements the ServletContextListener interface to listen to the creation and destruction of the ServletContext object

Package com. servlet. listener; import javax. servlet. servletContextEvent; import javax. servlet. servletContextListener;/*** implements the ServletContextListener interface to monitor the creation and destruction of the ServletContext object. * @ Author fan fangming */public class EasyServletContextListener implements ServletContextListener {@ Override public void contextInitialized (ServletContextEvent sce) {System. out. println ("----------- create ServletContext object") ;}@ Override public void contextDestroyed (ServletContextEvent sce) {System. out. println ("----------- ServletContext object destruction ");}}

4. modify web. xml


<Listener>
<Description> ServletContextListener listener </description>
<! -- Implements the listener class of the ServletContextListener interface -->
<Listener-class> com. servlet. listener. EasyServletContextListener </listener-class>
</Listener>

5. Running result
Start WEB middleware and see the console output:
---- Create a ServletContext object

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.