IT Band of Brothers JavaWeb tutorial listener 3

Monitor domain object property changes listeners

Domain object change event listener attribute is used to monitor the properties of these three objects in the ServletContext, HttpSession, HttpServletRequest change listener information about the event.

The three listener interfaces are ServletContextAttributeListener, HttpSessionAttributeListener and ServletRequestAttributeListener, three interfaces are defined in three ways to handle the increased property monitored objects, remove and replace the event, the same event at three interfaces the corresponding method of the exact same name, but different types of accepted parameters.

 

● attributeAdded way

When adding a property to a monitored object, web container calls the attributeAdded method of event listeners respond, this method receives an event type parameter, the listener can be obtained domain object is increasing properties by this parameter and the stored the properties of the object domain.

The complete syntax definition for each domain attribute listener as follows:

public void attributeAdded(ServletContextAttributeEvent event)

public void attributeAdded(HttpSessionBindingEvent event)

public void attributeRemove(ServletRequestAttributeEvent event)

● attributeRemove way

When you delete a property is listening object, attributeRemoved method web container calls event listeners to respond.

The complete syntax definition for each domain attribute listener as follows:

public void attributeRemoved(ServletContextAttributeEvent event)

public void attributeRemoved(HttpSessionBindingEvent event)

public void attributeRemoved(ServletRequestAttributeEvent event)

 

● attributeReplaced method

● When the listener domain object in an attribute to be replaced, web container calls attributeReplaced method of event listeners respond.

The complete syntax definition for each domain attribute listener as follows:

public void attributeReplaced(ServletContextAttributeEvent event)

public void attributeReplaced(HttpSessionBindingEvent event)

public void attributeReplaced(ServletRequestAttributeEvent event)

ServletContextAttributeListener listener examples:

● prepared ServletContextAttributeListener listener listening ServletContext domain object attribute value changes, as follows:

package com.xdl.listener;

import java.text.MessageFormat;

import javax.servlet.ServletContextAttributeEvent;

import javax.servlet.ServletContextAttributeListener;

/**

* MyServletContextAttributeListener class implements

* ServletContextAttributeListener Interface

* So you can listen for changes in the properties of the domain object ServletContext

*/

public class MyServletContextAttributeListener

    implements ServletContextAttributeListener {

    @Override

    public void attributeAdded(ServletContextAttributeEvent event) {

     String str = MessageFormat.format(

         "Added the ServletContext domain object attributes: {0}, the attribute value is: {1}",

         event.getName(), event.getValue());

     System.out.println(str);

    }

    @Override

    public void attributeRemoved(ServletContextAttributeEvent event) {

     String str = MessageFormat.format(

         "Remove the ServletContext domain object attributes: {0}, the attribute value is: {1}",

         event.getName(), event.getValue());

     System.out.println(str);

    }

    public void attributeReplaced(ServletContextAttributeEvent event) {

     String str = MessageFormat.format(

         "ServletContext replace the domain object attribute: value {0}", event.getName ());

     System.out.println(str);

    }

}

● registered listeners in the web.xml file

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns="http://xmlns.jcp.org/xml/ns/javaee"

    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

    version="3.1">

    <listener>

        <description>MyServletContextAttributeListener监听器</description>

        <listener-class>

             com.xdl.listener.MyServletContextAttributeListener

         </listener-class>

    </listener>

</web-app>

● write ServletContextAttributeListenerTest.jsp test page

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<Title> Band of Brothers IT Education </ title>

</head>

<body>

    <%

        // add a property to the application domain object

        application.setAttribute ( "name", "thirty draw students");

        // Replace the value of the name attribute in the application domain objects

        application.setAttribute ( "name", "twenty draw students");

        // remove the application domain objects in the name of the property

        application.removeAttribute("name");

    %>

</body>

</html>

Open Tomcat server, the results shown in Figure 12.

c8588c3829cb4252ba6f26dbe63718e2.png

FIG 12 MyServletContextAttributeListener output information in the console

 

Can be seen from the results of the run, ServletContextListener listener listens to the success of the ServletContext domain object (application) changes in property values.

ServletRequestAttributeListener and HttpSessionAttributeListenenr listeners Example:

 

● prepared listener listening HttpSession and HttpServletRequest domain object attribute value changes, as follows:

package com.xdl.listener;

import java.text.MessageFormat;

import javax.servlet.ServletRequestAttributeEvent;

import javax.servlet.ServletRequestAttributeListener;

import javax.servlet.http.HttpSessionAttributeListener;

import javax.servlet.http.HttpSessionBindingEvent;

/**

* MyRequestAndSessionAttributeListener class implements

* HttpSessionAttributeListener和ServletRequestAttributeListener接口

* So you can listen for changes HttpSession and ServletRequest domain object properties

*/

public class MyRequestAndSessionAttributeListener

 implements HttpSessionAttributeListener, ServletRequestAttributeListener {

 @Override

 public void attributeAdded(ServletRequestAttributeEvent event) {

    String str = MessageFormat.format(

         "Added to the ServletRequest domain object attributes: {0}, the attribute value is: {1}",

         event.getName(), event.getValue());

    System.out.println(str);

 }

 @Override

 public void attributeRemoved(ServletRequestAttributeEvent event) {

    String str = MessageFormat.format(

         "Remove the ServletRequest domain object attributes: {0}, the attribute value is: {1}",

         event.getName(), event.getValue());

    System.out.println(str);

 }

 @Override

 public void attributeReplaced(ServletRequestAttributeEvent event) {

    String str = MessageFormat.format(

         "ServletRequest replace the domain object attribute: value {0}", event.getName ());

    System.out.println(str);

 }

 @Override

 public void attributeAdded(HttpSessionBindingEvent event) {

    String str = MessageFormat.format(

         "Added the HttpSession domain object attributes: {0}, the attribute value is: {1}",

         event.getName(), event.getValue());

    System.out.println(str);

 }

 @Override

 public void attributeRemoved(HttpSessionBindingEvent event) {

    String str = MessageFormat.format(

         "To remove the HttpSession domain object attributes: {0}, the attribute value is: {1}",

         event.getName(), event.getValue());

    System.out.println(str);

 }

 @Override

 public void attributeReplaced(HttpSessionBindingEvent event) {

    String str = MessageFormat.format(

         "HttpSession replace the domain object attributes: {0} is", event.getName ());

    System.out.println(str);

 }

}

● registered listeners in the web.xml file

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns="http://xmlns.jcp.org/xml/ns/javaee"

    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

    version="3.1">

    <listener>

        <description>MyRequestAndSessionAttributeListener监听器</description>

        <listener-class>

             com.xdl.listener.MyRequestAndSessionAttribute Listener

        </listener-class>

    </listener>

</web-app>

● write RequestAndSessionAttributeListenerTest.jsp test page

<%@ page language="java" contentType="text/html; charset=UTF-8"

     pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<Title> Band of Brothers IT Education </ title>

</head>

<body>

    <%

        // add a property to the domain object in session

        session.setAttribute ( "name", "thirty draw students");

        // Replace the value of the name attribute domain session object

        session.setAttribute ( "name", "twenty draw students");

        // remove the session object domain name property

        session.removeAttribute("name");

        // add a property to request the domain object

        request.setAttribute ( "name", "thirty draw students");

        // Replace the value of the name attribute of the object request field

        request.setAttribute ( "name", "twenty draw students");

        // Remove request domain object name attribute

        request.removeAttribute("name");

    %>

   </body>

</html>

Open Tomcat server, the results shown in Figure 13.

97fba8589c064481a5bb9b4d8597b1b4.png

FIG 13 MyRequestAndSessionAttributeListener output information in the console

 

Can be seen from the results of the run, HttpSessionAttributeListeren listener and ServletRequestAttribute Listeren listener listens to the success of the property value changes of the domain object HttpSession and HttpServletRequest domain objects.

Guess you like

Origin www.cnblogs.com/itxdl/p/10961946.html