- 监听器
- 监听器概述
- 监听器概述
- 监听器概述
三大组件: Servlet、Filter、Listener
Listener -- 监听器: (GUI )
> 它是一个接口,内容由我们自己来实现
> 它需要注册,例如注册在按钮上
> 监听器中的方法,会在特殊事件发生时被调用
车被偷事件分析:
- 事件源:车 btn
- 事件:车被偷 按钮被点击
- 监听器:警察 listener
监听器中的方法: 实施抓捕 actionPerformed
-
- 开发监听器
- javaweb中编写监听器
- 开发监听器
- 写一个监听器类: 要求必须实现对应的监听器接口,如:
public class MyXxxListener implements XxxListener{...}
- 注册监听: 在web.xml中配置完成注册
<listener>
<listener-class>cn.tedu.listener.MyXxxListener</listener-class>
</listener>
-
- javaweb中的监听器: (八个!)
事件源: 三大域(ServletContext、ServletRequest、HttpSession)
-
-
- ServletContext(掌握)
-
(1) 生命周期监听(生死监听): ServletContextListener,有两个方法, 一个在出生时调用, 一个在死亡时调用
void contextInitialized(ServletContextEvent sce) 创建ServletContext时调用
void contextDestroyed(ServletContextEvent sce) 销毁ServletContext是调用
(2) 属性监听: ServletContextAttributeListener,有三个方法, 一个在添加属性时调用, 一个在替换属性时调用,一个在移除属性时调用
ServletContext sc = this.getServletContext();
sc.setAttribute(“a”, “xx”);
sc.setAttribute(“a”, “yyy”);
sc.removeAttribute(“a”);
void attributeAdded(ServletContextAttributeEvent scae) 添加属性时调用
void attributeReplaced(ServletContextAttributeEvent scae) 替换属性时调用
void attributeRemoved(ServletContextAttributeEvent scae) 删除属性时调用
-
-
- HttpSession(了解!!)
-
(1) 生命周期监听(生死监听): HttpSessionListener,有两个方法,一个在出生时调用, 一个在死亡时调用
void sessionCreated(HttpSessionEvent se) 创建Session时调用
void sessionDestroyed(HttpSessionEvent se) 销毁Session时调用
(2) 属性监听: HttpSessionAttributeListener, 有三个方法,一个在添加属性时调用, 一个在替换属性时调用,一个在移除属性时调用
void attributeAdded(HttpSessionBindingEvent se) 添加属性时调用
void attributeReplaced(HttpSessionBindingEvent se) 替换属性时调用
void attributeRemoved(HttpSessionBindingEvent se) 删除属性时调用
-
-
- ServletRequest(了解!!)
-
(1) 生命周期监听(生死监听): ServletRequestListener,有两个方法,一个在出生时调用, 一个在死亡时调用
void requestInitialized(ServletRequestEvent sre) 创建request时调用
void requestDestroyed(ServletRequestEvent sre) 销毁request时调用
(2) 属性监听: ServletRequestAttributeListener, 有三个方法,一个在添加属性时调用, 一个在替换属性时调用,一个在移除属性时调用。
void attributeAdded(ServletRequestAttributeEvent srae) 添加属性时调用
void attributeReplaced(ServletRequestAttributeEvent srae) 替换属性时调用
void attributeRemoved(ServletRequestAttributeEvent srae) 删除属性时调用
-
-
- ServletContextListener监听器案例
-
监听器案例:重写WEB应用中获取WEB应用虚拟路径的方式。
在WEB应用被加载之后,服务器会立即创建代表当前WEB应用的ServletContext对象, ServletContext对象创建之后激活ServletContextListener监听器,调用 contextInitialized()来进行处理, 在这个方法被调用时, 可以将WEB应用的虚拟路径存入到ServletContext域中, 方便后期在jsp中获取!!
在jsp中获取WEB应用的虚拟路径 : ${ pageContext.request.contextPath } /day17
通过监听器改变获取获取WEB应用的虚拟路径的方式: ${ app } /day17
ServletContext :
-
- 事件对象(了解)
- ServletContextEvent
- 事件对象(了解)
事件源:ServletContext
事件对象:ServletContextEvent
事件对象提供的方法:getServletContext(); //获取当前事件源
监听器接口:ServletContextListener
-
-
- HttpSessionEvent
-
事件源: HttpSession
事件对象: HttpSessionEvent
事件对象提供的方法: getSession(); //获取当前事件源
监听器接口: HttpSessionListener(HttpSession的生命周期监听)
-
-
- ServletRequestEvent
-
事件源: ServletRequest
事件对象: ServletRequestEvent
事件对象提供的方法:
getServletRequest(); //获取当前事件源
getServletContext(); //获取代表当前WEB应用的ServletContext对象
监听器接口: ServletRequestListener
-
-
- ServletContextAttributeEvent
-
事件源: ServletContex
事件对象: ServletContextAttributeEvent
事件对象提供的方法:
String getName(); //获取属性名
Object getValue(); //获取属性值
getServletContext(); //获取当前事件源
监听器接口: ServletContextAttributeListener
-
-
- HttpSessionBindingEvent
-
事件源: HttpSession
事件对象: HttpSessionBindingEvent
事件对象提供的方法:
String getName(); //获取属性名
Object getValue(); //获取属性值
getSession(); //获取当前事件源
监听器接口: HttpSessionAttributeListener
-
-
- ServletRequestAttributeEvent
-
事件源: ServletRequest
事件对象: ServletRequestAttributeEvent
事件对象提供的方法:
String getName(); //获取属性名
Object getValue(); //获取属性值
getServletRequest(); //获取当前事件源
getServletContext(); //获取代表当前WEB应用的ServletContext对象
监听器接口: ServletRequestAttributeListener
-
- 使JavaBean感知自己在Session域中状态变化的监听器
* 这两个监听器是用来添加到javaBean上,而不是三大域上!
* 这两个监听器不需要写专门的类去实现,也不需要在web.xml中注册
* 只要让javaBean自己实现监听器接口即可
-
-
- HttpSessionBindingListener
-
添加到JavaBean上, JavaBean就知道自己是否被添加到Session中了。
HttpSessionBindingListener提供的方法:
valueBound(HttpSessionBindEvent e); //当前JavaBean感知到自己被添加到Session时调用。
valueUnbound(HttpSessionBindEvent e); //当前JavaBean感知到自己被移出Session时调用
User user = new User();
HttpSession session = request.getSession();
session.setAttribute("user", user);
session.removeAttribute(“user”);
-
-
- HttpSessionActivationListener
-
添加到JavaBean上,JavaBean就知道自己是随着Session一起钝化了还是活化了
HttpSessionActivationListener提供的方法:
sessionWillPassivate(HttpSessionEvent e); //当前JavaBean感知自己随着Session一起钝化时调用
sessionDidActive(HttpSessionEvent e); //当前JavaBean感知自己随着Session一起活化时调用