Javaweb Basic---> Listener Listener (Forward)

Source: Internet
Author: User
Tags in domain

Listener 1, Basic concepts in Javaweb

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.

2, the servlet Listener classification

Several types of listeners are defined in the servlet specification, which are used to listen for the event sources ServletContext,HttpSession , and servletrequest , respectively, of three domain objects
The servlet specification also divides several types of listeners into three types for operations on these three objects:

    1. Listens for event listeners that are created and destroyed by the domain object itself.
    2. Listens for added and deleted event listeners for properties in domain objects.
    3. Listens for event listeners that are bound to the state of an object in the HttpSession domain.
3. 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 fired when the ServletContext object is created.

The contextdestroyed (Servletcontextevent SCE) method is fired when the ServletContext object is destroyed.

ServletContext domain object creation and destruction time:
Create: Server start create servletcontext for each web App
Destroy: Close the ServletContext on behalf of each Web application before the server shuts down

Example: Writing a Myservletcontextlistener class, implementing the Servletcontextlistener interface, monitoring the creation and destruction of ServletContext objects

3-1, write the Listener , the code is as follows:

 PackageCom.hanqi.maya.listener;Importjavax.servlet.ServletContextEvent;ImportJavax.servlet.ServletContextListener;/*** @ClassName: myservletcontextlistener* @Description: Myservletcontextlistener class implements Servletcontextlistener interface, * Therefore, the creation and destruction of the ServletContext object can be monitored for two actions. **/  Public classMyservletcontextlistenerImplementsServletcontextlistener {@Override Public voidcontextinitialized (Servletcontextevent sce) {System.out.println ("ServletContext Object Creation"); } @Override Public voidcontextdestroyed (Servletcontextevent sce) {System.out.println ("ServletContext Object Destruction"); }}

3-2. Registering listeners in the Web. xml file

As we mentioned in the above, to listen to the source of the event, you must register the listener to the event source to enable monitoring of the event source's behavior, in Javaweb, the registration of the listener is configured in the Web. xml file, as follows:

<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "3.0"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">    <!--registering listeners for listening on ServletContext objects -  <Listener>      <Description>Servletcontextlistener Listener</Description>      <!--a listener class that implements the Servletcontextlistener interface -      <Listener-class>Com.hanqi.maya.listener.MyServletContextListener</Listener-class>  </Listener>  </Web-app>

After these two steps, we completed the listener writing and registration, the Web server at startup, The listener configured in Web. XML is automatically registered to the ServletContext object, so that a well-developed Myservletcontextlistener listener can listen to the ServletContext object.

4. Monitoring the creation and destruction of HttpSession domain objects

Httpsessionlistener interface for monitoring the creation and destruction of HttpSession objects
When creating a session, the sessioncreated (httpsessionevent se) method is fired
Fires the sessiondestroyed (httpsessionevent se) method when a session is destroyed.

Example: Writing a Myhttpsessionlistener class, implementing the Httpsessionlistener interface, monitoring the creation and destruction of HttpSession objects

4-1, write the Listener , the code is as follows:

 PackageCom.hanqi.maya.listener;Importjavax.servlet.http.HttpSessionEvent;ImportJavax.servlet.http.HttpSessionListener;/*** @ClassName: myhttpsessionlistener* @Description: The Myhttpsessionlistener class implements the Httpsessionlistener interface, * therefore The creation and destruction of httpsession objects can be monitored by two actions. **/  Public classMyhttpsessionlistenerImplementsHttpsessionlistener {@Override Public voidsessioncreated (httpsessionevent se) {System.out.println (se.getsession ()+ "Created!! "); }    /*the destruction time of httpsession needs to be configured in Web. XML, as follows: * <session-config> <session-timeout>1</session-ti Meout> </session-config> This configuration means that the session will be destroyed after 1 minutes .*/@Override Public voidsessiondestroyed (httpsessionevent se) {System.out.println ("The session was destroyed!!" "); }}

4-2. Registering listeners in the Web. xml file

<!--registering listeners for listening on HttpSession objects -   <Listener>      <Description>Httpsessionlistener Listener</Description>      <Listener-class>Com.hanqi.maya.listener.MyHttpSessionListener</Listener-class>  </Listener>  <!--Configure the destroy time of the HttpSession object -  <Session-config>      <!--1 minutes after configuring the HttpSession object for destruction -      <Session-timeout>1</Session-timeout>  </Session-config>

When we visit the JSP page, the HttpSession object is created, and at this point we can observe the creation of the HttpSession object in Httpsessionlistener, and we can write a JSP page to observe the HttpSession object creation process.

5. Monitoring the creation and destruction of ServletRequest domain objects

Servletrequestlistener interface for monitoring the creation and destruction of ServletRequest objects
When the request object is created, the Listener's requestinitialized (Servletrequestevent sre) method is called
When the request object is destroyed, the listener's requestdestroyed (Servletrequestevent sre) method is called

ServletRequest domain object creation and destruction time:
Create: The Request object is created every time the user accesses
Destroy: The request object will be destroyed at the end of the current visit

Example: Writing a Myservletrequestlistener class, implementing the Servletrequestlistener interface, monitoring the creation and destruction of ServletRequest objects

5-1, write the Listener , the code is as follows:

 PackageCom.hanqi.maya.listener;Importjavax.servlet.ServletRequestEvent;ImportJavax.servlet.ServletRequestListener;/*** @ClassName: myservletrequestlistener* @Description: Myservletrequestlistener class implements Servletrequestlistener interface, * Therefore, the creation and destruction of the ServletRequest object can be monitored for two actions. **/  Public classMyservletrequestlistenerImplementsServletrequestlistener {@Override Public voidrequestdestroyed (servletrequestevent SRE) {System.out.println (Sre.getservletrequest ()+ "Destroyed!! "); } @Override Public voidrequestinitialized (servletrequestevent SRE) {System.out.println (Sre.getservletrequest ()+ "Created!! "); }}

5-2. Registering listeners in the Web. xml file

  <!--registering listeners for listening on ServletRequest objects -  <Listener>      <Description>Servletrequestlistener Listener</Description>      <Listener-class>Com.hanqi.maya.listener.MyServletRequestListener</Listener-class>   </Listener>

Original Blogger Blog: http://www.cnblogs.com/xdp-gacl/p/3961929.html

Javaweb Basic---> Listener Listener (Forward)

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.