Servlet - Filter Filter

A, Filter : In a previous threshold servlet responsible for interception request and response;

Second, create a filter:

  1. Filter interface implemented; // tomcat9.0 previously implemented method 3, only achieved after doFilter method;

  2. A successor HttpFilter class; // implement doFilter method;

Three, the Filter Interface:

  Core methods:

  (. 1) the init (FileterConfig) : the Filter After instance, immediately calls the init () Initializes and performed only once;

  (2 ) Destory (): execution server is legitimate when closed;

  (3doFilter(HttpServletRequest req,HttpServletResponse res,FilterChain chain):

    When executed once per visit;

    // parameters: the FilterChain : filter chain objects

Four, FilterChain interfaces:

  Release Request: the doFilter (the ServletRequest, the ServletResponse);

5, configuration Filter:

  1. annotation-based configuration:

    @WebFilter("url-parrent")

    Path to filter resources: url-parrent

@WebFilter("/*")
public class FilterTest extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("filter........");
        chain.doFilter(request, response);
    }
}v

  2. Based web.xml configuration:

    <filter>
        <filter-name></filter-name>
        <filter-class></filter-class>
    </filter>
    <filter-mapping>
        <filter-name></filter-name>
        <!-- 要过滤的资源的路径 -->
        <url-parrent></url-parrent>
    </filter-mapping>

   3.url-parrent parameters:

    // Support levels: /xxx/xxx.jsp;

   1) Precision: /xxx.jsp; / xxx;

   2) Blur:

    .jsp * : All jsp;

    .do * : All servlet;

   3) All: / *;

 

Guess you like

Origin www.cnblogs.com/Tractors/p/11279678.html