监听器之属性监听器 | 学习笔记

简介: 快速学习监听器之属性监听器

开发者学堂课程【Filte(过滤器)、Listener(监听器)监听器之属性监听器】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址https://developer.aliyun.com/learning/course/38/detail/888


监听器之属性监听器


以 ServletContextAttributeListener 举例:

三个方法:

void  attributeAdded(ServletContextAttributeEvent event)

Receives notification that an attribute has been added to the ServletContext.

void  attributeRenoved(ServletcontextAttribut eEvent event)

Receives notification that an attribute has been removed from the ServletContext.

Void  attributeReplaced(ServletContextAttributeEvent event)

ServletContextAttributeEvent 可以获取

getServletContext 信息,还可以得到添加属性的名称和值

写第二个监听器类:

第一种方法:

paakage cn.itcast.web-listener,

import javax.servlet.ServletContextAttributeEvent;

public class BListener implements

ServletContextAttributeListener{

public void attributeAdded (ServletContextAttributeEvent

scab){

system.out.println ( “您向 application 中添加了一个名为"+

scab.getName()+",值为:"+scab.getValue()+"的属性");

}

public void attributeReplaced(ServletContextAttributeEventacab){

}

public void attributeRemowed(ServletContextAttributeEvent acab){

}

}

添加到主页

<%

application.setAttribute("xxx","XXX");

%>

访问

第二种方法:

package cn.itcast.web.listener;

import javax.servlet.servletContextAttributeEvent;

public class BListener implements

servletContextAttributeListener{

public void attributeAdded (ServletcontextAttributeEvent scab)

{

System.out.println("您向 application 中添加了一个名为"+ scab.getName () + ",值为:"+scab.getvalue () +"的属性");

public void attributeReplaced(ServletcontextAttributeEvent scab)  {System.out.println (scab.getName ( ) +"=" +

scab.getvValue() + ","+ scab.getServletContext ( )

.getAttribute (scab.getName ());

public void attributeRemoved(ServletContextAttributeEvent scab){

}

}

当<%

application.setAttribute ( "xxx","XXX");

%>

中的“XXX”改成“YYY”或“ZZZ”

第三种方法:

package cn.itcast.web.listener;

import javax.servlet.ServletContextAttributeEvent;

public glass BListener implements

ServletContextAttributeListener {

public void attributeAdded(servletcontextAttributeEvent scab){System.out.println("您向 application 中添加了一个名为" + scab.getName () + ",值为:"+scab-getValue ( +"的属性");}

public void attributeReplaced(ServletContextattributeEventscab){

System.out.println(scab.getName ()+"=" + scab.getValue()+","+

scab.getServletContext ().getAttribute(scab.getName () )) ;}

public void attributeRemoved(ServletContextAttributeEvent acab){

system.out.println (acab.getName() + "=" + scab.getvalue ());}

}

<%@taglib prerix-" o" uri-" http:// java.

sun.com/jsp/jstl/oore" %>

My JSP 'remove-jsp' starting page

<%

application . removeAttribute ("xxx")

%>

访问

相关文章
|
12月前
|
Java
25JavaWeb基础 - 监听器
25JavaWeb基础 - 监听器
56 1
|
5月前
|
移动开发 前端开发 Java
Flowable 任务监听器与执行监听器的介绍
Flowable 任务监听器与执行监听器的介绍
777 0
|
5月前
javaWeb监听器Listener -自定义监听器类实现
javaWeb监听器Listener -自定义监听器类实现
|
存储 缓存 JavaScript
|
程序员
监听器
监听器
106 0
Listener概述、监听对象的创建和销毁、监听域对象属性变化、监听会话相关的感知型监听器及监听器实现
Listener概述、监听对象的创建和销毁、监听域对象属性变化、监听会话相关的感知型监听器及监听器实现
88 0
QGS
|
存储 XML 安全
浅谈入门ServletListener监听器
所有的监听器中的方法,当指定时间发生后,则Web服务器调用该方法。 第一步:创建一个类,实现监听接口和接口方法。
QGS
123 0
|
设计模式 安全 Java
监听器和过滤器
监听器和过滤器
123 0
监听器和过滤器
|
Java Spring
监听器 - 基础篇
监听器 - 基础篇
187 0
监听器 - 基础篇
|
JavaScript 前端开发
v-on:绑定事件监听器
v-on:绑定事件监听器
141 0