A good memory is better than a bad pen. 42-javaweb Listener Object Domain property changes (6)

Source: Internet
Author: User
Tags event listener

A good memory is better than a bad pen. 42-javaweb Listener Object Domain property changes (6)
Javaweb event listeners that change the properties of the object domain, can be used to listen for ServletContext, HttpSession, httpservletrequest these three objects in a property change information event listener.
These three listener interfaces are Servletcontextattributelistener, Httpsessionattributelistener and Servletrequestattributelistener, respectively, Each of the three interfaces defines three methods to handle the addition, deletion, and substitution of the attributes in the Listener object, the same event in the three interfaces corresponding to the same method name, except that the accepted parameter types are different.
1. Attributeadded method
When an attribute is added to the listener, the Web container responds with the Attributeadded method that invokes the event listener, which receives an event type parameter that the listener can use to obtain the domain object that is increasing the property and the Property object that is saved to the domain
The complete syntax in each domain property listener is defined as:
1 public void attributeadded (Servletcontextattributeevent scae)
2 public void attributereplaced (Httpsessionbindingevent hsbe)
3 public void attributermoved (Servletrequestattributeevent srae)

2. Attributeremoved method
The Web container invokes the event listener's Attributeremoved method to respond when one of the properties in the Listener object is deleted
The complete syntax in each domain property listener is defined as:
1 public void attributeremoved (Servletcontextattributeevent scae)
2 public void attributeremoved (Httpsessionbindingevent hsbe)
3 public void attributeremoved (Servletrequestattributeevent srae)

3. Attributereplaced method
When a property in the listener's domain object is replaced, the Web container invokes the event listener's attributereplaced method to respond
The complete syntax in each domain property listener is defined as:
1 public void attributereplaced (Servletcontextattributeevent scae)
2 public void attributereplaced (Httpsessionbindingevent hsbe)
3 public void attributereplaced (Servletrequestattributeevent srae)

4. Write the source code that listens to the property change of ServletContext domain object
Implement Servletcontextattributelistener interface and listen for changes in Object domain properties

 PackageCom.servlet.listener;ImportJava.text.MessageFormat;ImportJavax.servlet.ServletContextAttributeEvent;ImportJavax.servlet.ServletContextAttributeListener;/** * Simple implementation of Servletcontextattributelistener interface, listening to changes in the object domain properties * @author Fan Fangming */ Public  class easyservletcontextattributelistener  implements  Servletcontextattributelistener{    @Override     Public void attributeadded(Servletcontextattributeevent scab) {String str =messageformat.format ("The attribute was added to the ServletContext domain object: {0}, the property value is: {1}", Scab.getname (), Scab.getvalue ());    System.out.println (str); }@Override     Public void attributeremoved(Servletcontextattributeevent scab) {String str =messageformat.format ("Delete attribute in ServletContext domain object: {0}, property value is: {1}", Scab.getname (), Scab.getvalue ());    System.out.println (str); }@Override     Public void attributereplaced(Servletcontextattributeevent scab) {String str =messageformat.format ("The attribute was replaced in the ServletContext domain object: The value of {0}, the original value is {1}", Scab.getname (), Scab.getvalue ());    System.out.println (str); }}

5. Modify Web. xml

<listener>      <description>Easyservletcontextattributelistener Listener</Description>      <listener-class>Com.servlet.listener.EasyServletContextAttributeListener</listener-class>  </listener>"' 6, index.jsp for testing<%@ page language="java" import="java.util.*" pageencoding= "UTF-8"%><! DOCTYPE html><html><head><title>My JSP ' index.jsp ' starting page</title></head><body>HttpSession after the creation of the ID is: ${pagecontext.session.id}<br>    <%//Add the attribute Application.setattribute ("name", "FFM") to the application domain object;        Replace the value of the name attribute in the application domain object Application.setattribute ("name", "123");    Remove the name attribute Application.removeattribute ("name") from the application domain object; %></body></html>

7. Operation Result
Start Web middleware, access from URL:
http://127.0.0.1:8080/webStudy/index.jsp
Console output:
The ServletContext property is added to the domain object: Name, the property value is: FFM
The ServletContext Field object replaces the property: The value of name, the original value is FFM
Delete attribute in ServletContext domain object: Name, property value is: 123

8. Other
Because these three object domains are implemented similarly, the other two specific implementations can also fully reference the source code;

A good memory is better than a bad pen. 42-javaweb Listener Object Domain property changes (6)

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.