Servlet Filter, servletfilter

Source: Internet
Author: User

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>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.