Listener--域对象属性变化监听器([ServletRequest | HttpSession | ServletContext] AttributeListener)API详解

监听三大域对象的属性变化的监听器

  • ServletRequestAttributeListener
  • ServletContextAttributeListener
  • HttpSessionAttributeListener

域对象的通用方法:

  • setAttribute(name, value):
    触发添加属性的监听器的方法
    触发修改属性的监听器的方法

  • getAttribute(name):

  • removeAttribute(name):
    触发删除属性的监听器的方法

ServletContextAttibuteListener监听器的方法

//监听添加属性的方法
@Override
public void attributeAdded(ServletContextAttributeEvent scab){
	//获取到添加的属性
	System.out.println("添加属性:" + scab.getName() + ":" + scab.getValue());
}
//监听删除属性的方法
@Override
public void attributeRemoved(ServletContextAttributeEvent scab){
	//获取到删除的属性
	System.out.println("删除属性:" + scab.getName() + ":" + scab.getValue());
}
//监听修改属性的方法
@Override
public void attributeReplaced(ServletContextAttributeEvent scab){
	//获取到修改的属性
	System.out.println("修改属性:" + scab.getName() + ":" + scab.getValue());
}

HttpSessionAttributeListener监听器(同上)

ServletRequestAriibuteListenr监听器(同上)

实例:

监听器:

package pers.zhang.listener;

import javax.servlet.*;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 * @author zhang
 * @date 2019/9/20 - 8:53a
 */


public class MyAttributeListener implements ServletRequestAttributeListener, HttpSessionAttributeListener, ServletContextAttributeListener {

    //监听ServletContext属性添加
    @Override
    public void attributeAdded(ServletContextAttributeEvent scab) {
        System.out.println("添加属性:" + scab.getName() + ":" + scab.getValue());
    }

    //监听ServletContext属性删除
    @Override
    public void attributeRemoved(ServletContextAttributeEvent scab) {
        System.out.println("删除属性:" + scab.getName() + ":" + scab.getValue());
    }

    //监听ServletContext属性修改
    @Override
    public void attributeReplaced(ServletContextAttributeEvent scab) {
        System.out.println("修改属性:" + scab.getName() + ":" + scab.getValue());
    }

    //监听ServletRequest属性增加
    @Override
    public void attributeAdded(ServletRequestAttributeEvent srab) {
        System.out.println("添加属性:" + srab.getName() + ":" + srab.getValue());
    }

    //监听ServletRequest属性删除
    @Override
    public void attributeRemoved(ServletRequestAttributeEvent srab) {
        System.out.println("删除属性:" + srab.getName() + ":" + srab.getValue());
    }

    //监听ServletRequest属性修改
    @Override
    public void attributeReplaced(ServletRequestAttributeEvent srab) {
        System.out.println("修改属性:" + srab.getName() + ":" + srab.getValue());
    }

    //监听HttpSession属性增加
    @Override
    public void attributeAdded(HttpSessionBindingEvent hsb) {
        System.out.println("增加属性:" + hsb.getName() + ":" + hsb.getValue());
    }

    //监听HttpSession属性删除
    @Override
    public void attributeRemoved(HttpSessionBindingEvent hsb) {
        System.out.println("删除属性:" + hsb.getName() + ":" + hsb.getValue());
    }

    //监听HttpSession属性修改
    @Override
    public void attributeReplaced(HttpSessionBindingEvent hsb) {
        System.out.println("修改属性:" + hsb.getName() + ":" + hsb.getValue());
    }
}

注册:

    <listener>
        <listener-class>pers.zhang.listener.MyAttributeListener</listener-class>
    </listener>

Servlet:

package pers.zhang.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author zhang
 * @date 2019/9/20 - 10:16
 */
public class MyAtttibuteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //request域
        //增加属性
        request.setAttribute("request","addRequest");
        //修改属性
        request.setAttribute("request", "updateRequest");
        //删除属性
        request.removeAttribute("request");
        System.out.println("************************************************************");

        //sesion域
        HttpSession session = request.getSession(true);
        //增加属性
        session.setAttribute("session", "addSession");
        //修改属性
        session.setAttribute("session", "updateSession");
        //删除属性
        session.removeAttribute("session");
        System.out.println("************************************************************");

        //servletContext域
        ServletContext servletContext = getServletContext();
        //增加属性
        servletContext.setAttribute("servletContext", "addServletContext");
        //修改属性
        servletContext.setAttribute("servletContext", "updateServletContext");
        //删除属性
        servletContext.removeAttribute("servletContext");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

配置:

    <servlet>
        <servlet-name>myAttributeServlet</servlet-name>
        <servlet-class>pers.zhang.servlet.MyAtttibuteServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>myAttributeServlet</servlet-name>
        <url-pattern>/myAttribute</url-pattern>
    </servlet-mapping>

启动服务器,并访问/myAttribute,控制台输出:

修改属性:org.apache.catalina.ASYNC_SUPPORTED:true
添加属性:request:addRequest
修改属性:request:addRequest
删除属性:request:updateRequest
************************************************************
增加属性:session:addSession
修改属性:session:addSession
删除属性:session:updateSession
************************************************************
添加属性:servletContext:addServletContext
修改属性:servletContext:addServletContext
删除属性:servletContext:updateServletContext
上一篇:ServletContext 对象


下一篇:Tomcat+Spring中有几个ApplicationContext?它们的关系是什么样的?