开发者学堂课程【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")
%>
访问