The JavaWeb Filter: Filters

A, Filter Overview

  1, the concept of

    The web filter: when the resource access server, the filter may be intercepted request, complete some special features.

  2, the role of

    The general use for completing the operation. Such as: login authentication, unified coding process, sensitive character and other functions.

Second, the basic use

  1, the definition of a class that implements the interface Filter

  2, rewrite method

  3, intercept path configuration

    ① web.xml configuration

    ② arranged comment

  4, code implementation

 1 import javax.servlet.*;
 2 import javax.servlet.annotation.WebFilter;
 3 import java.io.IOException;
 4 
 5 @WebFilter("/*")
 6 public class FirstFilter implements Filter {
 7     public void destroy() {
 8     }
 9 
10     public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
11         System.out.println("filterDemo1被执行了....");
12         // 放行操作
13         chain.doFilter(req, resp);
14     }
15 
16     public void init(FilterConfig config) throws ServletException {
17 
18     }
19 
20 }

 

Third, details of the filter

  1, how to configure filter?

    (1) Configuration notes

       Add @WebFilter () can be in the class, which fill access path to be intercepted resources.

    (2) web.xml configuration

. 1    < filter > 
2          < filter-name > the demo1 </ filter-name > // the name of the filter 
. 3          < filter-class > cn.ks.web.filter.FilterDemo1 </ filter-class > // full class name 
4      </ filter > 
. 5      < filter-Mapping > 
. 6          < filter-name > the demo1 </ filter-name > 
. 7          <-! knockdown path -> 
. 8          < URL-pattern > / * </url-pattern>
9     </filter-mapping>

 

  2, execution flow filter?

    (1) perform filter

    (2) the implementation of the resources to be released

    (3) back release execute code following code filter

  3, filter life cycle approach?

    (1) init method: After the server is started, it will create a Filter object, and then call the init method. Performed only once, usually used to load resources.

    (2) doFilter method: every resource request is intercepted, it will perform. Executed multiple times

    (3) destory: After closing the server, Filter object is destroyed. If the server is shut down properly, it will perform the destroy method. Performed only once, typically used for releasing resources.

  4, filter configuration in detail?

    (1) intercept path configuration

       1. Specific resource path: /index.jsp only access index.jsp resources, the filter will be executed

       2. block a directory: When / admin / * access to all resources under / user, the filter will be executed

       3. intercept extension: * .jsp suffix named jsp access to all resources, the filter will be executed

       4. intercept all resources: / * to access all resources, the filter will be executed

    (2) intercept mode configuration : the way resources are accessed

       Configuration notes :

          Set dispatcherTypes property

          

Property name Explanation
REQUEST Defaults. Direct browser requests a resource
FORWARD Forwarding access to resources
INCLUDE It includes access to resources
ERROR Error Jump Resources
ASYNC Asynchronous access to resources

       web.xml configuration :

           设置 <dispatcher></dispatcher>标签,在里面填入上面的值即可。

  5、过滤器链(配置多个过滤器)

    (1)执行顺序:如果有两个过滤器:过滤器1和过滤器2

       ① 过滤器1

       ② 过滤器2

       ③ 资源执行

       ④ 过滤器2

       ⑤ 过滤器1

    (2)过滤器先后顺序问题

       ① 注解配置:

        规则:按照类名的字符串比较规则比较,值小的先执行。

        如: AFilter 和 BFilter,AFilter就先执行了。

       ② web.xml 配置:

        规则: <filter-mapping>谁定义在上边,谁先执行

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11627551.html