The filter (Filter) of the three Java devices (Filter/listener/(Interceptor))
Interceptor : It is aspect-oriented programming , that is, calling a method before your service or a method, or calling a method after the method, which is based on the reflection mechanism of JAVA. For example, a dynamic proxy is a simple implementation of an interceptor. It prints a string before you call a method (or does other business logic operations), or prints a string after you call a method, or even when you throw an exception. Operation of business logic.
servlet, filter and listener are configured in web.xml (the loading order of web.xml is: context-param -> listener -> filter -> servlet), the interceptor is not configured in web.xml, the interceptor of struts is configured to struts.xml. Spring's interceptors are configured in spring.xml.
Filter, filter , used to modify the request and response outside the servlet. Filter has a concept of FilterChain, a FilterChain includes multiple Filters. The client request request will go through all the Filters in the FilterChain before reaching the servlet, and the server will also go through all the Filters in the FilterChain before the server responds from the servlet to the client browser.
Introduction to Filter
Filter, also known as filter, is the most practical technology in Servlet technology. Web developers use Filter technology to intercept all web resources managed by the web server: such as Jsp, Servlet, static image files or static html files, etc. To achieve some special functions. For example, it implements some advanced functions such as URL-level permission access control, filtering sensitive words, and compressing response information.
It is mainly used to pre-process user requests, and can also post-process HttpServletResponse. The complete process of using Filter: Filter preprocesses user requests, then hands the request to Servlet for processing and generates a response, and finally Filter performs post-processing on the server response.
Filter function
Before the HttpServletRequest reaches the servlet, intercept the client's HttpServletRequest. Check the HttpServletRequest as needed, and also modify the HttpServletRequest headers and data.
Intercept the HttpServletResponse before it reaches the client. Check the HttpServletResponse as needed and also modify the HttpServletResponse headers and data.
Implementation of Filter
1. Implementing a custom Filter needs to meet the following conditions:
1) Implement the javax.servlet.Filter interface and implement its three methods of init, doFilter, and destroy.
2) Implement the configuration in web.xml.
2. javax.servlet.Filter interface
1) The Filter interface has three methods: These three methods reflect the life cycle of Filter.
①, init: It will only be called when the web program is loaded, that is, when the server such as tomcat is started. Generally responsible for loading configuration parameters.
②, destroy: called when the web program is uninstalled. Generally responsible for shutting down certain containers, etc.
③, doFilter: It will be called once for each client request. All the work of Filter is basically concentrated in this method.
Configure Filter: Each filter needs to be configured in web.xml to take effect. A Filter needs to be configured with <filter> and <filter-mapping> tags.
1) <filter>: Configure Filter name, implementation class and initialization parameters. Multiple initialization parameters can be configured at the same time.
2) <filter-mapping>: Configure what rules to use this Filter under.
①, <url-pattern>: The rules for configuring url, you can configure multiple, or you can use wildcards (*). For example, /jsp/* applies to all servlet paths starting with "/jsp/" under this ContextPath, and *.do applies to all servlet paths ending with ".do".
②, <dispatcher>: Configure the way to reach the servlet, you can configure multiple at the same time. There are four values: REQUEST, FORWARD, ERROR, INCLUDE. If not configured, it defaults to REQUEST. Their differences are:
# REQUEST : Indicates that it only takes effect when the servlet is requested directly.
# FORWARD : Indicates that it only takes effect when a servlet is forwarded to the servlet through forward.
# INCLUDE : A servlet can be requested through <jsp:include/> in Jsp, only in this case it is valid.
# ERROR: The error handling page can be specified by <%@page errorPage="error.jsp" %> in Jsp, and it will only take effect in this case.
③. The relationship between <url-pattern> and <dispatcher> is and, only if the conditions of <url-pattern> are met and the conditions of <dispatcher> are met, the Filter can take effect.
<!-- filter configuration -->
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>servlet.filter.MyFilter</filter-class>
<init-param>
<param-name>name</param-name>
<param-value>Sam-Sho</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/jsp/*</url-pattern>
<url-pattern>*.do</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
3) Summary: A Web program can configure multiple Filters, and the access is sequential. The Filter configured in the front of <filter-mapping> is executed earlier than the Filter configured in the back.
Common Filter
Character encoding filter
Anti-leech Filter --> Anti-leech needs to use the request header Referer
Permission Check Filter
File Upload Filter
GZIP Compression Filter
//Get the compression format supported by the browser
String acceptEncoding = request.getHeader("Accept-Encoding");
System.out.println("Accept-Encoding: " + acceptEncoding);
if (acceptEncoding != null && acceptEncoding.toLowerCase().indexOf("gzip") != -1) {
// If the client browser supports the GZIP format, use GZIP to compress the data
GZipResponseWrapper gzipResponse = new GZipResponseWrapper(response);
chain.doFilter(request, gzipResponse);
// output compressed data
gzipResponse.getOutputStream();
gzipResponse.finishResponse();
} else {
// otherwise, do not compress
chain.doFilter(request, response);
}