Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Servlet Filter

Shulou Source: shulou.com Published: 2022-06-01 03:19:08 09月29日 Update

Filter:

Filter in Java is not a standard Servlet. It cannot handle user requests or generate responses to clients. It is mainly used for pre-processing of HttpServletRequest and post-processing of HttpServletResponse, which is a typical processing chain.

The advantage of the filter chain is that it can be interrupted at any time during execution, and subsequent filters and requests will not be executed as long as chain.doFilter () is not executed.

1. Filter driver:

When the web application starts, the web server creates each registered Filter instance object based on the configuration information in the web.xml file and saves it in the server's memory

HelloFilter

Com.jalja.filter.HelloFilter

HelloFilter

/ freeMark

2. Filter API:

/ / destory () is called before the Web container unloads the Filter object. This method is executed only once in the life cycle of the Filter. In this method, you can release the resources used by the filter.

Public void destroy ()

/ / execution of Filter chain

/ / chain.doFilter (request, response); / / pass the request to the next filter, and if the filter is the last filter of the filter chain, send the request to the target resource

Public void doFilter (ServletRequest request, ServletResponse response,FilterChain chain)

/ / the init method similar to servlet is called when the Filter object is created, only once, and initializes the current filter. Filter singleton

/ / FilterConfig is similar to servletConfig to obtain initialization parameter information of Filter.

Public void init (FilterConfig filterConfig)

3. Filter intercept sequence:

When multiple filter are configured in web.xml, the filter chain is formed, and their interception order depends on the order.

4. Filter filters request resources by default. If it is a request in other ways, the filter is invalid. If you want to use it, you need to add it in to specify the filtering method.

REQUEST

FORWARD

INCLUDE

ERROR

Tags: Objects methods resources filters order processing information methods servers services configuration typical memory content parameters depending on cycle multiple benefit instance Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Apple MariaDB vpn MySQL