zoukankan      html  css  js  c++  java
  • Servlet3.0新特性WebFilter(Annotation Filter)详解

    摘要:

    Servlet3.0作为J2EE 6规范一部分,并随J2EE6一起发布,WeFilter是过滤器注解,是Servlet3.0的新特性,不需要在web.xml进行配置,简化了配置。

    Name

    Type

    Required

    Description

    filterName

    String

    Optional

    Name of the filter.

    value

    or

    urlPatterns

    String[]

    Required

    Specify one or more URL patterns to which the filter applies. Either of attribute can be used, but not both.

    dispatcherTypes

    DispatcherType[]

    Optional

    Specify types of dispatcher to which the filter applies. Default isjavax.servlet.DispatcherType.REQUEST

    servletNames

    String[]

    Optional

    Specify names of servlets to which the filter applies.

    displayName

    String

    Optional

    Display name of the filter.

    description

    String

    Optional

    Description of the filter.

    asyncSupported

    boolean

    Optional

    Specify whether the filter supports asynchronous operation mode. Default is false.

    initParams

    WebInitParam[]

    Optional

    Specify one or more initialization parameters of the filter. Each parameter is specified by@WebInitParamannotation type.

    smallIcon

    String

    Optional

    Specify name of the small icon of the filter.

    largeIcon

    String

    Optional

    Specify name of the large icon of the filter.

    OneFilter:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    package com.what21.servlet.webfilter;
     
    import java.io.IOException;
     
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebFilter;
     
    @WebFilter("/*")
    public class OneFilter implements Filter {
     
        @Override
        public void init(FilterConfig config) throws ServletException {
     
        }
     
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            System.out.println("OneFilter doFilter()");
            chain.doFilter(request, response);
        }
     
        @Override
        public void destroy() {
     
        }
     
    }

    TwoFilter:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    package com.what21.servlet.webfilter;
     
    import java.io.IOException;
     
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebFilter;
     
    @WebFilter(servletNames = "MyFourServlet")
    public class TwoFilter implements Filter {
     
        @Override
        public void init(FilterConfig config) throws ServletException {
     
        }
     
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            System.out.println("TwoFilter doFilter()");
            chain.doFilter(request, response);
        }
     
        @Override
        public void destroy() {
     
        }
     
    }

    ThreeFilter:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    package com.what21.servlet.webfilter;
     
    import java.io.IOException;
     
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebFilter;
     
    @WebFilter(servletNames = {"MyFourServlet", "MyFiveServlet"})
    public class ThreeFilter implements Filter {
     
        @Override
        public void init(FilterConfig config) throws ServletException {
     
        }
     
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            System.out.println("ThreeFilter doFilter()");
            chain.doFilter(request, response);
        }
     
        @Override
        public void destroy() {
     
        }
     
    }

    FourFilter:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    package com.what21.servlet.webfilter;
     
    import java.io.IOException;
     
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebFilter;
     
    @WebFilter({"/one","/two"})
    public class FourFilter implements Filter {
     
        @Override
        public void init(FilterConfig config) throws ServletException {
     
        }
     
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            System.out.println("FourFilter doFilter()");
            chain.doFilter(request, response);
        }
     
        @Override
        public void destroy() {
     
        }
     
    }

    FiveFilter:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    package com.what21.servlet.webfilter;
     
    import java.io.IOException;
     
    import javax.servlet.DispatcherType;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebFilter;
    import javax.servlet.annotation.WebInitParam;
     
    @WebFilter(
        urlPatterns = "/five",
        filterName = "FiveFilter",
        initParams = {
            @WebInitParam(name = "name", value = "username"),
            @WebInitParam(name = "value", value = "password")
        },
        description = "MyFiveFilter",
        dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD}
    )
    public class FiveFilter implements Filter {
     
        @Override
        public void init(FilterConfig config) throws ServletException {
            String name = config.getInitParameter("name");
            String value = config.getInitParameter("value");
            System.out.println("name = " + name);
            System.out.println("value = " + value);
            System.out.println("init().....");
        }
     
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            System.out.println("FiveFilter doFilter()");
        }
     
        @Override
        public void destroy() {
     
        }
     
    }

      

  • 相关阅读:
    python学习===从一个数中分解出每个数字
    python学习===复制list
    Jmeter===测试案例参考
    Jmeter==HTTP信息头管理器的作用
    python实战===使用随机的163账号发送邮件
    python实战===实现读取txt每一行的操作,账号密码
    python实战===生成随机数
    python实战===输入密码以******的形式在cmd中展示
    python实战===使用smtp发送邮件的源代码,解决554错误码的问题,更新版!
    python实战===使用smtp发送邮件的源代码,解决554错误码的问题
  • 原文地址:https://www.cnblogs.com/xingzc/p/7519507.html
Copyright © 2011-2022 走看看