1 监听器
1.1 监听器概述
1.1.1 监听器概述
三大组件: Servlet、Filter、Listener
Listener -- 监听器: (GUI )
> 它是一个接口,内容由我们自己来实现
> 它需要注册,例如注册在按钮上
> 监听器中的方法,会在特殊事件发生时被调用
车被偷事件分析:
Ø 事件源:车 btn
Ø 事件:车被偷 按钮被点击
Ø 监听器:警察 listener
监听器中的方法: 实施抓捕 actionPerformed
1.2 开发监听器
1.2.1 javaweb中编写监听器
1. 写一个监听器类: 要求必须实现对应的监听器接口,如:
public class MyXxxListener implements XxxListener{...}
2. 注册监听: 在web.xml中配置完成注册
<listener>
<listener-class>cn.tedu.listener.MyXxxListener</listener-class>
</listener>
1.3 javaweb中的监听器: (八个!)
类名 接口 事件类型 场景
javax.servlet.ServletContextListener contextInitialized/contextDestroyed ServletContextEvent 你想知道是否创建或销毁了一个上下文。
javax.servlet.ServletRequestListener requestInitialized /requestDestroyed ServletRequestEvent 每次请求到来时你都想知道,想建立访问日志。
javax.servlet.http.HttpSessionListener sessionCreated/sessionDestroyed HttpSessionEvent 你想知道有多少个并发用户。也就是说,服务器当前有多少个会话,这些会话何时创建何时销毁。
javax.servlet.http.HttpSessionActivationListener HttpSessionEvent 在会话对象迁移到另一个JVM时得到通知。(用于分布式环境)
javax.servlet.http.HttpSessionBindingListener HttpSessionBindingEvent 监视会话的属性变化,或监视某些对象与会话的关系变化。
javax.servlet.http.HttpSessionAttributeListener : 监听session的属性变化
Javax.servlet.servletrequestattributeListen:监听request请求的属性变化
javax.servlet.ServletContextAttributeListener :监视应用上下文的属性变化。
事件源: 三大域(ServletContext、ServletRequest、HttpSession)
1.3.1 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) 删除属性时调用
1.3.2 HttpSession(了解!!)
以下两种情况下就会发生sessionDestoryed(会话销毁)事件:
1.执行session.invalidate()方法时
2.如果用户长时间没有访问服务器,超过了会话最大超时时间 ,服务器就会自动销毁超时的session。
会话超时时间可以在web.xml中进行设置,为了容易看到超时效果,我们将超时时间设置为最小值
1 2 3 | <session-config> <session-timeout>1</session-timeout> </session-config> |
时间单位是一分钟,并且只能是整数,如果是零或负数,那么会话就永远不会超时
(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) 删除属性时调用
1.3.3 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) 删除属性时调用
1.3.4 ServletContextListener监听器案例
监听器案例:重写WEB应用中获取WEB应用虚拟路径的方式。
在WEB应用被加载之后,服务器会立即创建代表当前WEB应用的ServletContext对象, ServletContext对象创建之后激活ServletContextListener监听器,调用 contextInitialized()来进行处理, 在这个方法被调用时, 可以将WEB应用的虚拟路径存入到ServletContext域中, 方便后期在jsp中获取!!
在jsp中获取WEB应用的虚拟路径 : ${ pageContext.request.contextPath } /day17
通过监听器改变获取获取WEB应用的虚拟路径的方式: ${ app } /day17
ServletContext :
public void contextInitialized(ServletContextEvent sce) {
//可以通过事件对象(sce)获取当前事件源
ServletContext context = sce.getServletContext();
//context.getContextPath() 相当于 request.getContextPath()
context.setAttribute("app", context.getContextPath());
System.out.println("当前WEB应用的虚拟路径已被存入ServletContext域中...");
}
1.4 事件对象(了解)
1.4.1 ServletContextEvent
事件源:ServletContext
事件对象:ServletContextEvent
事件对象提供的方法:getServletContext(); //获取当前事件源
监听器接口:ServletContextListener
1.4.2 HttpSessionEvent
事件源: HttpSession
事件对象: HttpSessionEvent
事件对象提供的方法: getSession(); //获取当前事件源
监听器接口: HttpSessionListener(HttpSession的生命周期监听)
1.4.3 ServletRequestEvent
事件源: ServletRequest
事件对象: ServletRequestEvent
事件对象提供的方法:
getServletRequest(); //获取当前事件源
getServletContext(); //获取代表当前WEB应用的ServletContext对象
监听器接口: ServletRequestListener
1.4.4 ServletContextAttributeEvent
事件源: ServletContex
事件对象: ServletContextAttributeEvent
事件对象提供的方法:
String getName(); //获取属性名
Object getValue(); //获取属性值
getServletContext(); //获取当前事件源
监听器接口: ServletContextAttributeListener
1.4.5 HttpSessionBindingEvent
事件源: HttpSession
事件对象: HttpSessionBindingEvent
事件对象提供的方法:
String getName(); //获取属性名
Object getValue(); //获取属性值
getSession(); //获取当前事件源
监听器接口: HttpSessionAttributeListener
1.4.6 ServletRequestAttributeEvent
事件源: ServletRequest
事件对象: ServletRequestAttributeEvent
事件对象提供的方法:
String getName(); //获取属性名
Object getValue(); //获取属性值
getServletRequest(); //获取当前事件源
getServletContext(); //获取代表当前WEB应用的ServletContext对象
监听器接口: ServletRequestAttributeListener
1.5 使JavaBean感知自己在Session域中状态变化的监听器
* 这两个监听器是用来添加到javaBean上,而不是三大域上!
* 这两个监听器不需要写专门的类去实现,也不需要在web.xml中注册
* 只要让javaBean自己实现监听器接口即可
1.5.1 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”);
1.5.2 HttpSessionActivationListener
添加到JavaBean上,JavaBean就知道自己是随着Session一起钝化了还是活化了
HttpSessionActivationListener提供的方法:
sessionWillPassivate(HttpSessionEvent e); //当前JavaBean感知自己随着Session一起钝化时调用
sessionDidActive(HttpSessionEvent e); //当前JavaBean感知自己随着Session一起活化时调用
public class User implements HttpSessionBindingListener,Serializable,HttpSessionActivationListener {。。。}