backgroundIn the process of program implementation we often encounter a problem similar to this: in order to prevent the background to receive data can not be garbled, we need to set the request character sets in the servlet processing, we will add such code in each servlet:
Request.setcharacterencoding ("GB18030");
This code is written in the program is dead, the function of setting character set is not good for maintenance, if you want to modify in the future to another character set type, need to be modified in each servlet. If you are encapsulating, you also face the problem that each servlet calls, and often this method invocation is not related to the specific business requirements. In this case, it is not possible to resolve it in a normal programmatic way. For this kind of problem, the paper introduces the filter technique similar to the AOP idea.
Basic IntroductionFilter is a newly added feature of the Servlet2.3 specification. Enables developers to change the request before the user requests reach the servlet or to leave the servlet in response to change the response.
Filter Effect:
1. Original schema implementation diagram
2. Add Filter
We have added a filter class between Tomcat and servlet, a matching rule in the Web-info configuration, and a corresponding configuration to initialize the Init method that invokes filter when the Tomcat starts, and when the client operates, the rule-compliant operation Then Tomcat automatically calls the Dofilter method for interception, and similarly, we can manipulate the response object.
<!--filter configuration, corresponding to the filter implementation class--><filter><!--filter Name-- <filter-name> charsetencodingfilter</filter-name><!--corresponding Implementation class- <filter-class> com.tgb.drp.util.filter.charsetencodingfilter</filter-class><!--initialization parameters, similar to key-value pair Access- < init-param> <param-name>encoding</param-name> <param-value>gbk</param-value > </init-param> </filter><!--matching rule configuration-- <filter-mapping> < Filter-name>charsetencodingfilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping>
Filter and responsibility chain mode
The specific use of a single filter is discussed earlier, and how does it embody the responsibility chain model? Let's take a look at the structure of the responsibility chain pattern:
Responsibility chain mode, through the client side set the specific processor class of the home and the next, through the implementation of abstract class abstraction method HandleRequest to specifically deal with a specific situation of the request, if not within the scope of processing, then call home for processing.
In the application of filter technology, the filter interface corresponds to the handler abstract class, and we do not have to call the Setsuccessor method, The upper and lower call relationships of filter can be specified in Web. XML, so the Dofilter method directly calls the Chain.dofilter method to execute the successor's Dofilter method, thus the filter technology is the embodiment of more advanced application of the responsibility chain pattern.
Filter and Servelt
As can be seen from the above, the filter and the servlet is a layer of the relationship between the package, it by intercepting the user's request to the servlet to process some requests or response data information in advance or post.
Summary
Filter is often used for permission control, logging, conversion coding format, encryption and other specific business processing coupling is not high, we can think of it as a filter to implement the plug-in, but also image as a filter, filter. It provides a declarative service with the ability to plug in and pull it up for better reuse.
References
Java Filter Filtering mechanism detailed
Execution order of Java filter
Learning from filter, Servlet, and Listener in Java
"Java Technology drip"--filter basic use and expansion