JavaWeb学习:十二、监听器Listener

监听域对象自身创建和销毁的监听器:
①ServletContextListener接口 监听 SercvletContext对象

②HttpSessionListener接口 监听 HttpSession对象

③ServletRequestListener接口 监听 ServletRequest对象

监听域对象中的属性的增加、修改和删除的事件监听器
④ServletContextAttributeListener接口 监听 SercvletContext对象属性

⑤HttpSessionAttributeListener接口 监听 HttpSession对象属性

⑥ServletRequestAttributeListener接口 监听 ServletRequest对象属性

监听绑定到 HttpSession 域中某个对象的状态的事件监听器
⑦HttpSessionBindingListener接口 监听 实现了HttpSessionBindingListener接口的对象的session绑定和解除

⑧HttpSessionActivationListener接口 (实现会话的持久化)

监听器的包含类及其作用

①ServletContextListener
相关方法
contextInitialized(ServletContextEvent arg0):在Web应用加载的时候被调用

contextDestroyed(ServletContextEvent arg0): 在Web应用卸载的时候被调用

arg0.getServletContext()可获取当前应用的上下文对象

扫描二维码关注公众号,回复: 6221862 查看本文章

该监听器的作用:
对相关资源进行初始化工作,如创建数据库连接池、创建Spring IOC 容器、读取当前Web应用的初始化参数等

②HttpSessionListener
相关方法
sessionCreated(HttpSessionEvent arg0): Session创建时被调用

sessionDestroyed(HttpSessionEvent arg0): Session销毁时被调用

Session什么时候销毁?
关闭服务器、Session过期、手动调用session.invalidate()方法

注意:用户关闭浏览器时原有Session并不会销毁,会等到timeout超时自动销毁

该监听器的作用:
统计在线人数、记录访问日志等

③ServletRequestListener
相关方法
requestInitialized(ServletRequestEvent arg0): Request创建时被调用

requestDestroyed(ServletRequestEvent arg0): Request销毁时被调用

每次请求相应会创建一次和销毁一次

即每次刷新页面都会导致Request的创建和销毁

转发是一次响应,可以获取Request的信息

重定向是两次响应,即前一个页面的Request对象和重定向后的页面的Request对象不是同一个对象,因此不能获得前一个对象的Request信息

该监听器的作用:
读取参数,记录访问历史等

④ServletContextAttributeListener
相关方法
attributeAdded(ServletContextAttributeEvent arg0): request.getServletContext().setAttribute(“name”,“value”)初次创建调用

attributeReplaced(ServletContextAttributeEvent arg0): request.getServletContext().setAttribute(“name”,“newValue”)被修改时调用

attributeRemoved(ServletContextAttributeEvent arg0): 执行request.getServletContext().removeAttribute(“name”)时调用

以上三个方法中可用arg0.getName()获取属性名arg0.getValue()获取属性值,但是要注意在attributeReplaced()中获取的是旧的值

⑤HttpSessionAttributeListener
相关方法
attributeAdded(HttpSessionBindingEvent arg0): session.setAttribute(“name”, “sessionValue”)初次创建调用

attributeReplaced(HttpSessionBindingEvent arg0): session.setAttribute(“name”, “newSessionValue”)被修改时调用

attributeRemoved(HttpSessionBindingEvent arg0): 执行session.removeAttribute(“name”)时调用

以上三个方法中可用arg0.getName()获取属性名arg0.getValue()获取属性值,但是要注意在attributeReplaced()中获取的是旧的值

⑥ServletRequestAttributeListener
相关方法
attributeAdded(ServletRequestAttributeEvent arg0): request.setAttribute(“name”, “requestValue”)初次创建调用

attributeReplaced(ServletRequestAttributeEvent arg0): request.setAttribute(“name”, “newRequestValue”)被修改时调用

attributeRemoved(ServletRequestAttributeEvent arg0): 执行request.removeAttribute(“name”)时调用

以上三个方法中可用arg0.getName()获取属性名arg0.getValue()获取属性值,但是要注意在attributeReplaced()中获取的是旧的值

⑦HttpSessionBindingListener
前提:javaBean实现了该接口

相关方法
valueBound(HttpSessionBindingEvent arg0): session.setAttribute(“name”, javaBean)触发绑定方法

valueUnbound(HttpSessionBindingEvent arg0): session.removeAttribute(“name”)触发绑定解除方法

⑧HttpSessionActivationListener
实现会话的持久化

前提:实现该接口和序列化接口Serializable

可以感知自己被活化(从硬盘到内存)和钝化(从内存到硬盘)的过程

当服务器突然关闭,用户的session就不存在了,即用户就需要重新登录等操作,这样很麻烦,于是我们就需要实现会话的持久化来解决。

可以让我们在重新启动服务器之后用户的session还在服务器中存在

相关方法
sessionWillPassivate(HttpSessionEvent arg0): 钝化方法,关闭服务器调用的方法

可以将用户的Session储存到tomcat目录下的/work/Catalina/localhost/项目名 下的SESSION.ser文件中

sessionDidActivate(HttpSessionEvent arg0): 活化方法,重新启动服务器时调用

Session从硬盘回复到内存中,目录下的SESSION.ser文件小消失

猜你喜欢

转载自blog.csdn.net/lifeng6336785/article/details/90172661