美文网首页工作生活
04JavaWeb Day09_Listener_Filter

04JavaWeb Day09_Listener_Filter

作者: 雪之梦_8f14 | 来源:发表于2019-07-07 07:32 被阅读0次

监听器

  • 监听器: 监听某一个事件的发生或者状态的改变, 内部机制其实就是借口回调

  • 一共有8个监听器 可分为两种类型:需要注册的监听器和不需要注册的监听器

  • 需要注册的监听器又可以根据作用域的不同 划分为三类监听器

  • 三种作用域和对应的监听器分别是:request -- ServletRequestListener、 session -- HttpSessionListener、 application -- ServletContextListener

  • ServletContextListener

    • 创建: 启动服务器的时候
    • 销毁:正常关闭服务器的时候
  • ServletContextAttributeListener tomcat服务启动时会出现 serveletcontext 属性添加事件 貌似启动服务器后第一次访问jsp文件时会出现serveletcontext 属性添加事件

    • attributeAdded: applciation.addAttribute("name", "name");
    • attributeRemoved: applciation.removeAttribute("name");
    • attributeReplaced: applciation.addAttribute("name", "name");
  • ServletRequestListener

    • 创建:访问服务器上的任意资源都会被创建 html、jsp、servlet等
    • 销毁:服务器已对这次请求做出了响应
  • ServletRequestAttributeListener 只要访问jsp文件 就会 serveletRequest 属性替换事件 ???

    • attributeAdded: request.setAttribute("name", "smx");
    • attributeRemoved: request.removeAttribute("name");
    • attributeReplaced: request.setAttribute("name", "smx1");
  • HttpSessionListener 如果不按套路出牌 那么要清楚浏览器缓存 或者重新开一个浏览器

    • 创建:只要调用getSession() jsp默认会创建 但是servlet必须得调用getSession() 但是html不会
    • 销毁: 超时(30分钟) 非正常关闭 正常关闭服务器 (一直不能出现销毁过程 问题在哪?)
  • HttpSessionAttributeListener

    • attributeAdded: session.setAttribute("name", "smx");
    • attributeRemoved: session.setAttribute("name", "smx1");
    • attributeReplaced: session.removeAttribute("name");
  • 作用:

    • ServletContextListener

      利用它来,在servletcontext创建的时候,
      1. 完成自己想要的初始化工作
      2. 执行自定义任务调度。 执行某一个任务。 Timer

    • HttpSessionListener

      统计在线人数.

  • 不需要注册的监听器 (HttpSessionBindingListener和HttpSessionActivationListener)
    • HttpSessionBindingListener 监听对象与session 绑定和解除绑定 的动作

      • 只要一个类实现了 HttpSessionBindingListener 接口 然后实例对象被放进session的attribute中, 那么对应的事件就会执行
      • bind event: session.setAttribute("bind", bind);
      • unbind event: session.removeAttribute("bind"); 或者 tomcat服务器被关闭
    • HttpSessionActivationListener 用于监听现在session的值 是 钝化 (序列化)还是活化 (反序列化)的动作

      • 钝化(序列化):把内存中的数据存储到硬盘上 ??找不到硬盘上的文件??
      • 活化(反序列化): 把硬盘上的数据读取到内存中
      • session的钝化和活化的意义何在:session中的值可能会很多, 并且我们有很长一段时间不使用这个内存中的值, 那么可以考虑把session的值可以存储到硬盘上【钝化】,等下一次在使用的时候,在从硬盘上提取出来。 【活化】
      • DeActive:session.setAttribute("bean", bean); 关闭服务器的时候回钝化
      • Active:开启服务器后就会活化 访问钝化的属性 可以获取上次设置的值 但是我没能重现 原因可能是钝化失败 查明原因是 除了实现Listener接口外 还有实现Serializable 接口
      • 配置 让session在一定时间内钝化
        • 在tomcat里面 conf/context.xml 里面配置
          对所有的运行在这个服务器的项目生效
          • 在conf/Catalina/localhost/context.xml 配置
            对 localhost生效。 localhost:8080
            • 在自己的web工程项目中的 META-INF/context.xml
              只对当前的工程生效。
              maxIdleSwap : 1分钟不用就钝化
              directory : 钝化后的那个文件存放的目录位置。
D:\tomcat\apache-tomcat-7.0.52\work\Catalina\localhost\ListenerDemo\itheima
                    <Context>
                        <Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1">
                            <Store className="org.apache.catalina.session.FileStore" directory="itheima"/>
                        </Manager>
                    </Context>

Filter

  • 自定义过滤器
    • 定义一个类 实现Filter接口
    • 注册过滤器(类似于注册Servlet)
  • Filter 生命周期
    • 创建: 在服务器启动的时候创建
    • 注销: 服务器停止的时候注销
  • Filter 执行顺序
    • 客户端发出请求 先经过过滤器 如果过滤器放行 才能到servlet
    • 如果有多个过滤器, 那么他们按照注册的顺序来排队。只要有一个过滤器不放行,那么后面排队的过滤器以及servlet都不会收到请求
  • Filter细节
    • 可以通过 init方法中的参数 FilterConfig 获取Filter在注册的时候初始化的键值对 这里的设计与ServletConfig是一样的
    • 放行代码:```chain.doFilter(request, response)
    • urlpattern 写法和 servlet一样
    • 针对dispatcher设置:
      • REQUEST(默认): 只要是请求过来 ,都拦截
      • FORWARD: 只要是转发都拦截
      • ERROR: 页面出错发生挑战拦截
      • INCLUDE: 包含页面的时候就拦截

自动登录

  • 使用Filter 拦截请求 判断是否session中是否有用户的信息 若是没有的话 那么从cookie中获取用户信息 放到session中

BeanUtils

  • 所需jar包:commons-beanutils-1.8.3.jar 和 commons-logging-1.1.1.jar
  • 普通的转换代码
Map map = request.getParameterMap();
UserBean bean1 = new UserBean();
BeanUtils.populate(bean1, map);
  • 高级转换 需要自定义转换规则 例如 String转Date 需要先定义一个转换类, 然后再使用ConvertUtil.register 才行
public class MyDateConverter implements Converter {

    @Override
    // 将value 转换 c 对应类型
    // 存在Class参数目的编写通用转换器,如果转换目标类型是确定的,可以不使用c 参数
    public Object convert(Class c, Object value) {
        String strVal = (String) value;
        // 将String转换为Date --- 需要使用日期格式化
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = dateFormat.parse(strVal);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
//注册自己的日期转换器
ConvertUtils.register(new MyDateConverter(), Date.class);// 我自己实现报错 DateConverter does not support default String to 'Date' conversion.

相关文章

  • 04JavaWeb Day09_Listener_Filter

    监听器 监听器: 监听某一个事件的发生或者状态的改变, 内部机制其实就是借口回调 一共有8个监听器 可分为两种类...

  • 04JavaWeb Day13_Redis

    Redis使用 简单使用示例 连接服务器 ./redis-cli -h ip地址 -p 端口号(默认是6379) ...

  • 04JavaWeb Day10_基础增强

    基础加强 注解 annotation 什么是注解?注解和接口、类一样都属于数据类型 注解作用编译检查配置(后期用的...

网友评论

    本文标题:04JavaWeb Day09_Listener_Filter

    本文链接:https://www.haomeiwen.com/subject/rdiacctx.html