Servlet Filter, servletfilter
Filter:
The Filter in Java is not a standard Servlet. It cannot process user requests or generate responses to clients. It is mainly used to pre-process HttpServletRequest or HttpServletResponse. It is a typical processing chain.
The advantage of the filter chain is that the chain can be interrupted at any time during execution. As long as the chain. doFilter () is not executed, the following filters and request content will no longer be executed.
1. filter Driver:
When a web application is started, the web server creates each registered Filter instance object based on the configuration information in the web. xml file and saves it in the memory of the server.
<Filter>
<Filter-name> helloFilter </filter-name>
<Filter-class> com. jalja. filter. HelloFilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-name> helloFilter </filter-name>
<! -- The resource to be intercepted -->
<Url-pattern>/freeMark </url-pattern>
</Filter-mapping>
2. filter interface:
// Destory () is called before the Web Container uninstalls the Filter object. This method is only executed once in the lifecycle of the Filter. In this method, resources used by the filter can be released.
Public void destroy ()
// Filter chain execution
// Chain. doFilter (request, response); // send the request to the next filter. If the filter is the last filter of the filter chain, the request is sent to the target resource.
Public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain)
// Similar to the servlet init method, this method is called when a Filter object is created. It is called only once to initialize the current filter. The filter Singleton
// FilterConfig is similar to servletConfig to obtain the initialization parameter information of the Filter.
Public void init (FilterConfig filterConfig)
3. filter interception sequence:
When multiple filters are configured in web. xml, the filter chain is formed. Their interception order depends on the order of <filter-mapping>.
4. The filter filters the requested resources by default. If other methods are used, the filter is invalid. To use the filter, add <dispatcher> in <filter-mapping> to specify the filtering method.
<Dispatcher> REQUEST </dispatcher>
<Dispatcher> FORWARD </dispatcher>
<Dispatcher> INCLUDE </dispatcher>
<Dispatcher> ERROR </dispatcher>