zoukankan      html  css  js  c++  java
  • Filter

    The funciton of filter: filter the resouce and it's one of the Serlvet technology. When the user access to the resource, the filter will make some process for the request and response.

    How to use Filter?

      1. Create a class implements the Filter interface

      2. Modify the web.xml file's configuration and configure the filter's mapping:

      web.xml:

        <filter>
            <filter-name>FilterDemo1</filter-name>
            <filter-class>com.pp.filter.FilterDemo1</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>FilterDemo1</filter-name>
            <!-- Filter all the resource in the application -->
            <url-pattern>/*</url-pattern>
        </filter-mapping>

      3. Call the chain.doFilter(request, response) method to execute the resourece

    The life circle of Filter:

     The Filter instance is created by server and the method in Filter also called by server.

     The Filter will be complete the instantiation and call the init() method when the application loaded. Every request sent by user, the doFilter() method will be called. And the server will call the destroy() method when the application was removed or the server stopped.

    public class FilterDemo1 implements Filter {
    
        public FilterDemo1() {
            System.out.println("FilterDemo1's constructor");
        }
    
        public void destroy() {
            System.out.println("destroy the FilterDemo1");
        }
    
        // this method will be called when access resource
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            System.out.println("filter the FilterDemo1");
            // access to other resource
            chain.doFilter(request, response);
        }
    
        public void init(FilterConfig config) throws ServletException {
            System.out.println("initialize the FilterDemo1");
        }
    }

     Feature: the instance variables in Filter will lead to thread security problmes.

    The filter order: 

    The multi-filter execution order: the filter execution is executed by the filter-mapping configuring order in web.xml

    Servlet & Filter:

      1. The Filter has the same function of Servlet, in addition: the Filter has the chain.doFilter() function.

      2. In MVC, the Servlet acts as the Controller, and Filter also coule works as Controller.

      3. Struts1: Servlet serves as Controller  Struts2: Filter serves as Controller

        

  • 相关阅读:
    通过IMM With Remote Console为服务器安装操作系统
    linux下编译安装php5.6出现 configure: error: Cannot find MySQL header files under /usr/local/mysql.
    5700交换机清除配置
    嵌入式驱动解析:从串口驱动到Linux驱动模型
    Win10自带Ubuntu子系统的安装与配置
    关于嵌入式C代码优化的几种方法
    2020软考高级系统分析师,你想知道的全在这
    libpng warning: iCCP: known incorrect sRGB profile
    pycharm中导入pygame库失败及解决办法
    pycharm中导入pygame等第三方库
  • 原文地址:https://www.cnblogs.com/ppcoder/p/7510678.html
Copyright © 2011-2022 走看看