栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

案例:监听域对象的属性变更

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

案例:监听域对象的属性变更

创建测试页面testattribute.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>


测试域对象属性变更事件监听器


	这是一个测试域对象属性变更事件监听器的页面
	<%
		getServletContext().setAttribute("username", "itcast");
		getServletContext().setAttribute("username", "itheima");
		getServletContext().removeAttribute("username");
		session.setAttribute("username", "itcast");
		session.setAttribute("username", "itheima");
		session.removeAttribute("username");
		request.setAttribute("username", "itcast");
		request.setAttribute("username", "itheima");
		request.removeAttribute("username");
	%>


创建监听器MyAttributeListener
public class MyAttributeListener implements ServletContextAttributeListener,
		HttpSessionAttributeListener, ServletRequestAttributeListener {
	public void attributeAdded(ServletContextAttributeEvent sae) {
		String name = sae.getName();
		System.out.println("ServletContext添加属性:" + name + "="
				+ sae.getServletContext().getAttribute(name));
	}
	public void attributeRemoved(ServletContextAttributeEvent sae) {
		String name = sae.getName();
		System.out.println("ServletContext移除属性: " + name);
	}
	public void attributeReplaced(ServletContextAttributeEvent sae) {
		String name = sae.getName();
		System.out.println("ServletContext替换属性:" + name + "="
				+ sae.getServletContext().getAttribute(name));
	}
	public void attributeAdded(HttpSessionBindingEvent hbe) {
		String name = hbe.getName();
		System.out.println("HttpSession添加属性:" + name + "="
				+ hbe.getSession().getAttribute(name));
	}
	public void attributeRemoved(HttpSessionBindingEvent hbe) {
		String name = hbe.getName();
		System.out.println("HttpSession移除属性: " + name);
	}
	public void attributeReplaced(HttpSessionBindingEvent hbe) {
		String name = hbe.getName();
		System.out.println("HttpSession替换属性:" + name + "="
				+ hbe.getSession().getAttribute(name));
	}
	public void attributeAdded(ServletRequestAttributeEvent sra) {
		String name = sra.getName();
		System.out.println("ServletRequest添加属性:" + name + "="
				+ sra.getServletRequest().getAttribute(name));
	}
	public void attributeRemoved(ServletRequestAttributeEvent sra) {
		String name = sra.getName();
		System.out.println("ServletRequest移除属性: " + name);
	}
	public void attributeReplaced(ServletRequestAttributeEvent sra) {
		String name = sra.getName();
		System.out.println("ServletRequest替换属性:" + name + "="
				+ sra.getServletRequest().getAttribute(name));
	}
}
web.xml中配置监听器
  
    
		cn.itcast.chapter08.listener.MyAttributeListener
	
  
启动Tomcat,测试

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/583861.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号