Detailed spring filter

A, Filter basic working principle

  • 1, Filter program is a Java class that implements a special interface, similar to the Servlet, also called by the Servlet container and execution.
  • 2, when the register when a Filter to intercept a processing procedure in the web.xml Servlet, it can decide whether to pass on the request to the Servlet procedures, as well as request and response message is to be modified.
  • 3, when the vessel began to invoke a Servlet Servlet program, if found to have been registered for a program to intercept the Filter Servlet, then the vessel is no longer directly call the service method Servlet, but call doFilter method Filter, and then by the doFilter The method of deciding whether to activate the service method.
  • 4, but the method can not be directly called Filter.doFilter Servlet service method, but the method to activate the service call FilterChain.doFilter Servlet of a target, the object passed in parameter when FilterChain Filter.doFilter method.
  • 5, just before and after the call statement FilterChain.doFilter method of increasing process Filter.doFilter some program code, so that the response can be achieved before and after some special features in Servlet.
  • 6, if there is no call FilterChain.doFilter method Filter.doFilter method, the target Servlet's service method will not be executed, so that by Filter can block certain illegal access requests.

Two, Filter Chain

 

  • 1, in a Web application can register multiple Filter programs, each program can intercept Filter to a Servlet or a set of procedures. If multiple programs can Filter intercepts a Servlet program access procedure, when reaching a request for access to the Servlet, the Web Filter container combining a plurality of these programs as a chain Filter (also called the filter chain) .
  • 2, each order intercept Filter Filter the chain is consistent with their mapping order in the web.xml file on a method call FilterChain.doFilter Filter.doFilter method activates the next Filter doFilter method, and finally a Filter.doFilter FilterChain.doFilter methods call will activate the service method of the Servlet target.
  • 3, as long as any one of the chain Filter Filter does not call FilterChain.doFilter method, the target Servlet's service method will not be executed.

Three, Filter Interface

A Filter program is a Java class that must implement the Filter interface. javax.servlet.Filter interface defines three methods: init, doFilter, destory.

1, init method

    • (1), when the Web application starts, Web server (Web container) will be used to create an instance of each registered Filter according to configuration information of its web.xml file and save it in memory.
    • After (2), Web Filter container creates an instance of the object, it will call the init method of the Filter object immediately. The init method is executed only once, Web container when calling the init method, it passed FilterConfig object that contains configuration and runtime information Filter of the Filter life cycle.

      public voic init(FilterConfig filterConfig) throws ServletException
    • (3) Developers can complete the construction method similar to the initialization function init method, note that: If you want to use initialization code to objects FilterConfig, these codes can only be written in the init method, but can not write in the constructor (not yet call the init method, which does not create FilterConfig object that you want to use it is inevitable mistakes).

2, doFilter method

When an object is able to intercept an access request Filter, Servlet container calls the method doFilter Filter object.

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException.ServletException

Wherein the request and response parameters on the Web Filter container or a chain transfer Filter over request and response objects; chain represented by the current object parameter Filter chain.

3, destroy method

This method is called before uninstalling Web Filter object in a container, also performed only once. Complete opposite function init method, is opened to release the resources Filter object, for example: close the connection and the IO stream.

Four, FilterChain Interface

This interface is used to define a method objects Filter chain should be provided externally, this interface defines a method doFilter.

public void doFilter(ServletRequest request, ServletResponse response) throws java.io.IOException.ServletException

FilterChain doFilter interface method for notifying a request to the Web container next to a Filter processing Filter chain, Filter objects if the current invocation of this method is the last one in the chain Filter Filter, then the program will request to the target Servlet to deal with.

Five, FilterConfig Interface

  • 1, the same as ordinary Servlet program, Filter program is also likely to need access to Servlet container. Servlet specification ServletContext object representing the parameters and configuration information Filter encapsulated in objects called FilterConfig.
  • 2, FilterConfig interfaces define the methods FilterConfig objects should be provided for outside, so that the program can call Filter these methods to get ServletContext object, and get the friendly name and Filter initialization parameters set in the web.xml file.
  • 3, FilterConfig each interface definition Method:

    • getFilterName method, returns the value of <filter-name> element.

    • getServletContext method returns a reference to the object FilterConfig ServletContext packaged object.

    • getInitParameter method that returns a name for the parameter values ​​provided in the Filter web.xml file initialized.

    • getInitParameterNames method returns a collection of objects Enumeration.

 

Guess you like

Origin www.cnblogs.com/uoar/p/11058229.html