SPRINGMVC Framework is a well-established Java Web Development Framework, Learning Research framework design helps us better understand and master Spring MVC, design and write more conforming structure and code
Filter: In Javaweb, your incoming request, Response filter out some information in advance, or set some parameters ahead of time, and then pass in the servlet or struts action for business logic,
For example, filter out illegal URLs (not login.do address requests, if the user does not log on to filter out), or in the servlet or struts before the action of the uniform set of character sets, or to remove some illegal characters.
The Java files under Spring MVC's Org.springframework.web.filter package are as follows:
As shown in the Spring-web.jar package structure shown above, there are many filters available in spring's Web package, These filters are located in Org.springframework.web.filter and, of course, implement the Javax.servlet.Filter,
The structure of the class is as follows:
There are several ways to implement this:
(1) Directly realize filter, this kind of filter only compositefilter;
(2) Inheriting abstract class Genericfilterbean, this class implements the Javax.servlet.Filter, this kind of filter has only one, namely Delegatingfilterproxy;
(3) Inherit abstract class Onceperrequestfilter, which is the direct subclass of Genericfilterbean, which includes Characterencodingfilter, Hiddenhttpmethodfilter, Httpputformcontentfilter, Requestcontextfilter and Shallowetagheaderfilter;
(4) Inheriting abstract class Abstractrequestloggingfilter, which is a direct subclass of Onceperrequestfilter, which includes Commonsrequestloggingfilter, Log4jnesteddiagnosticcontextfilter and Servletcontextrequestloggingfilter.
Where is the filter placed in the container structure?
Before a filter is placed on a web resource, it can intercept incoming requests before the request arrives at the Web resource it applies to, which can be a servlet, a JSP page, or even an HTML page, and intercept the output request before it returns to the customer. Filter: Used to intercept requests between the client and the requested resource for the purpose of reusing code. The filter chain, which is first configured in Web. XML and which is called first. Some initialization parameters can also be configured in the filter.
Filter in Java is not a standard servlet, it cannot handle user requests, nor can it generate responses to clients. It is mainly used for pretreatment of HttpServletRequest, but also for post-processing of HttpServletResponse, which is a typical processing chain.
There are several types of Filter:
User-Authorized Filter:filter is responsible for checking the user request and filtering the user's illegal request according to the request.
L Log Filter: Detailed logging of certain special user requests.
The filter that is responsible for decoding: Includes request decoding for non-standard encoding.
L can change the xsltfilter of XML content and so on.
Filter has several uses:
L INTERCEPT the client's httpservletrequest before HttpServletRequest arrives at the servlet.
L Check the HttpServletRequest as needed, and you can also modify the HttpServletRequest header and data.
L Intercept HttpServletResponse before HttpServletResponse arrives at the client.
L Check the HttpServletResponse as needed, you can modify the HttpServletResponse header and data.
Creating a filter takes only two steps:
(1) Create a filter processing class:
(2) Configure filter in the Web. xml file.
The Create Filter must implement the Javax.servlet.Filter interface, which defines three methods in the interface.
? void init (filterconfig config): Used to complete the initialization of the filter.
? void Destroy (): the collection of some resources is completed before the filter is destroyed.
? void DoFilter (ServletRequest request, Servletresponse Response,filterchain chain): Implements the filtering function, which is the additional processing added to each request and response.
Filter filters also have a life cycle: init ()->dofilter ()->destroy (), driven by the filter element in the deployment file.
Refer to the encoding filter example to see how it is implemented
- The rules that are first configured in the Web. xml file are as follows
<!--encoding Processing filters-- <filter> <filter-name>encodingFilter</filter-name> < Filter-class>org.springframework.web.filter.characterencodingfilter</filter-class> <init-param > <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> < param-value>true</param-value> </init-param> </filter> <filter-mapping > <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Where Filter-class is the filter class, Init-prama is the set parameter injected
Url-pattern in filter-mapping is a filtered URL type
Inheritance relationships for classes
Characterencodingfilter R class inherits the Onceperrequestfilter class
public class Characterencodingfilter extends Onceperrequestfilter
And the Onceperrequestfilter class inherits the Genericfilterbean class.
Public abstract class Onceperrequestfilter extends Genericfilterbean
Public abstract class Genericfilterbean implements
Filter, Beannameaware, Environmentaware, Servletcontextaware, Initializingbean, Disposablebean
The core code of the set encoding is
@Override protected void dofilterinternal ( httpservletrequest request, httpservletresponse response, Filterchain Filterchain) throws Servletexception, IOException { if (this.encoding! = null && ( this.forceencoding | | request.getcharacterencoding () = = null) { request.setcharacterencoding (this.encoding); if (this.forceencoding) { response.setcharacterencoding (this.encoding); } } Filterchain.dofilter (request, response); }
Where Filterchain is the filter chain, which means that the next filter is executed after the filter is executed
Difference
When we use filters, we don't usually need to know Genericfilterbean, Onceperrequestfilter and Abstractrequestloggingfilter, but we don't know about these classes, as described above, Abstractrequestloggingfilter inherits from Onceperrequestfilter,onceperrequestfilter inherited from Genericfilterbean, so we know that Genericfilterbean is a more convenient superclass of any type of filter, and this class basically implements the value set in Init-param from the Web. xml file, and then initializes the filter (of course, its subclasses can override the Init method).
Onceperrequestfilter inherits from Genericfilterbean, then it naturally knows how to get the properties and their values in the configuration file, so the focus is not on the value, but on ensuring that once a request is received, each filter is executed only once, Its subclasses only need to focus on the specific implementation of filter dofilterinternal.
Abstractrequestloggingfilter is an extension of onceperrequestfilter, which, in addition to all the functions of its parent and ancestor classes, determines the events that are executed before and after filtering in dofilterinternal. Its subclasses are concerned with Beforerequest and afterrequest.
In general, these three classes perform one part of the filter function, and of course, how to do it by their subclasses, and if you need to implement your own filters, you can inherit the classes you need, as described above.
This article transferred from: http://blog.csdn.net/sdmxdzb/article/details/45221531
Springmvc Filters Filter Usage parsing