监听器(Listener)

监听对象:request、session、application

a.监听对象的创建和销毁
request:ServletRequestListener
session:HttpSessionListener;
application:ServletContextListener
b.监听对象中属性的变更
request:ServletRequestAttributeListener;
session:HttpSessionAttributeListener;
application:SevletContextAttributeListener;

监听器xml配置
<listener>
       <listener-class></listener-class>
</listener>
package org.student.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class AttributeListener2 implements ServletRequestAttributeListener,HttpSessionAttributeListener,ServletContextAttributeListener{

	@Override
	public void attributeAdded(HttpSessionBindingEvent e) {
		String name = e.getName();
		Object obj = e.getSession().getAttribute(name);
		System.out.println("HttpSession【增加】属性名:" + name + ",属性值:" + obj);
	}

	@Override
	public void attributeRemoved(HttpSessionBindingEvent e) {
		System.out.println("HttpSession【删除】属性名:" + e.getName());
	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent e) {
		String name = e.getName();
		Object obj = e.getSession().getAttribute(name);
		System.out.println("HttpSession【替换】属性名:" + name + ",属性值:" + obj);
	}

	@Override
	public void attributeAdded(ServletRequestAttributeEvent e) {
		String name = e.getName();
		Object obj = e.getServletRequest().getAttribute(name);
		System.out.println("ServletRequest【增加】属性名:" + name + ",属性值:" + obj);
	}

	@Override
	public void attributeRemoved(ServletRequestAttributeEvent e) {
		System.out.println("ServletRequest【删除】属性名:" + e.getName());
	}

	@Override
	public void attributeReplaced(ServletRequestAttributeEvent e) {
		String name = e.getName();
		Object obj = e.getServletRequest().getAttribute(name);
		System.out.println("ServletRequest【替换】属性名:" + name + ",属性值:" + obj);
	}

	@Override
	public void attributeAdded(ServletContextAttributeEvent e) {
		String name = e.getName();
		Object obj = e.getServletContext().getAttribute(name);
		System.out.println("SevletContext【增加】属性名:" + name + ",属性值:" + obj);
	}

	@Override
	public void attributeRemoved(ServletContextAttributeEvent e) {
		System.out.println("SevletContext【删除】属性名:" + e.getName());
	}

	@Override
	public void attributeReplaced(ServletContextAttributeEvent e) {
		String name = e.getName();
		Object obj = e.getServletContext().getAttribute(name);
		System.out.println("SevletContext【替换】属性名:" + name + ",属性值:" + obj);
	}

	
}
package org.student.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MyListener implements ServletRequestListener,HttpSessionListener,ServletContextListener{

	//Request
	@Override
	public void requestDestroyed(ServletRequestEvent e) {
		System.out.println("监听ServletRequest,销毁:" + e);
	}

	@Override
	public void requestInitialized(ServletRequestEvent e) {
		System.out.println("监听SevletRequest,创建:" + e);
	}

	//application
	@Override
	public void contextDestroyed(ServletContextEvent e) {
		System.out.println("监听ServletContext,销毁:" + e);
	}

	@Override
	public void contextInitialized(ServletContextEvent e) {
		System.out.println("监听SevletContext,创建:" + e);
	}

	//session
	@Override
	public void sessionCreated(HttpSessionEvent e) {
		System.out.println("监听HttpSession,创建:" + e);
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent e) {
		System.out.println("监听HttpSession,销毁:" + e);
	}
	
}
监听器(Listener)

原创文章,作者:LeoRiver,如若转载,请注明出处:https://www.hgjun.cn/?p=1136

发表评论

电子邮件地址不会被公开。 必填项已用*标注

联系我们

QQ-501439094

撩一撩:点击这里给我发消息

邮件:501439094@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

隐藏