首页 > 其他分享 >过滤器Filter和监听器Listener

过滤器Filter和监听器Listener

时间:2023-02-23 04:11:06浏览次数:34  
标签:Listener public Filter 监听器 Integer onlineCount CharacterEncodingFilter

1、Filter(重点)

Filter:过滤器,用来过滤网站的数据

  • 处理中文乱码
  • 登录验证
  • ...

Filter开发步骤:

  1. (pom文件中)导包

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
    
    <!--连接数据库-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.32</version>
        </dependency>
    
    </dependencies>
    
  2. 编写过滤器(解决乱码用“resp.setContentType("text/html;charset=utf-8");”

    1. 导包不要错

    2. 实现Filter接口,重写对应的方法即可

      public class CharacterEncodingFilter implements Filter {
          // 初始化(web服务器启动,就已经执行了,随时等待过滤对象)
          @Override
          public void init(FilterConfig filterConfig) throws ServletException {
              System.out.println("CharacterEncodingFilter初始化");
          }
      
          // Chain:链
          /*
          1.过滤器中的所有代码,在过滤特定请求的时候都会执行
          2.必须要让过滤器继续通行
              filterChain.doFilter(servletRequest, servletResponse);
           */
          @Override
          public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
              servletRequest.setCharacterEncoding("utf-8");
              servletResponse.setCharacterEncoding("utf-8");
              servletResponse.setContentType("text/html;charset=utf-8");
      
              System.out.println("CharacterEncodingFilter执行前。。。");
              filterChain.doFilter(servletRequest, servletResponse); // 放行作用,让我们的请求继续走,如果不写,程序到这里就被拦截停止了!
              System.out.println("CharacterEncodingFilter执行后。。。");
          }
      
          // 销毁(web服务器关闭的时候执行)
          @Override
          public void destroy() {
              System.out.println("CharacterEncodingFilter销毁");
          }
      }
      
    3. 在web.xml中配置

      <filter>
          <filter-name>CharacterEncodingFilter</filter-name>
          <filter-class>com.kuang.filter.CharacterEncodingFilter</filter-class>
      </filter>
      <filter-mapping>
          <filter-name>CharacterEncodingFilter</filter-name>
          <!--只要是/servlet的任何请求,都会经过这个过滤器-->
          <url-pattern>/servlet/*</url-pattern>
          <!-- <url-pattern>/*</url-pattern>  这样写,整个网站的请求都会走过滤器-->
      </filter-mapping>
      

2、监听器

实现一个监听器的接口(有N种)

  1. 编写一个监听器

    实现监听器的接口

    // 统计网站在线人数:统计Session
    public class OnlineCountListener implements HttpSessionListener {
        // 创建Session监听:看它的一举一动
        // 一旦创建一个Session就会触发一次这个事件
        @Override
        public void sessionCreated(HttpSessionEvent httpSessionEvent) {
            ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
            Integer onlineCount = (Integer) servletContext.getAttribute("OnlineCount");
    
            System.out.println(httpSessionEvent.getSession().getId());
    
            if (onlineCount == null){
                onlineCount = new Integer(1);
            }else{
                int count = onlineCount.intValue();
    
                onlineCount = new Integer(count + 1);
            }
    
            servletContext.setAttribute("OnlineCount",onlineCount);
        }
    
        // 销毁Session监听
        // 一旦销毁Session就会触发一次这个事件
        @Override
        public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
            ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
            Integer onlineCount = (Integer) servletContext.getAttribute("OnlineCount");
    
            if (onlineCount == null){
                onlineCount = new Integer(0);
            }else{
                int count = onlineCount.intValue();
    
                onlineCount = new Integer(count - 1);
            }
    
            servletContext.setAttribute("OnlineCount",onlineCount);
        }
    }
    
  2. web.xml中注册监听器

    <!--  注册监听器  -->
    <listener>
        <listener-class>com.kuang.listener.OnlineCountListener</listener-class>
    </listener>
    
  3. 看情况是否使用!

标签:Listener,public,Filter,监听器,Integer,onlineCount,CharacterEncodingFilter
From: https://www.cnblogs.com/jiaxing-java/p/17146589.html

相关文章