Overview:
The filter is a newly added function of Servlet2.3 and its technology is also very powerful. The Filter technology can intercept WEB server files to implement some special functions. It is also one of the essential skills in JSP application development. Filter can change a request and a response ). A Filter is not a Servlet and cannot generate a response. It can pre-process a request before a requsst arrives at the Servlet, or process response when it leaves the Servlet.
Principle:
When a Web Container receives a request for a resource, it determines whether a filter is associated with the resource. If yes, the container sends the request to the filter for processing. In the filter, you can change the request content, or reset the request information, and then send the request to the target resource. After the target resource responds to the request, the container also forwards the response to the filter first, the filter can convert the response content and then send the response to the client.
In a Web application, you can deploy multiple filters to form a filter chain. Each filter in the filter chain is responsible for specific operations and tasks, and client requests are passed between these filters until the target resource. The WEB server depends on the Filter in the web. the registration order in the xml file determines which Filter to call first. When the doFilter method of a local Filter is called, the WEB server will create a FilterChain object of the Filter chain and pass it to the method.
Note: filters are not a standard Servlet and cannot process user requests or generate responses to clients. It is mainly used to pre-process HttpServletRequest or HttpServletResponse.
HttpServletRequest and HttpServletResponse Workflow
First, you can intercept the customer's HttpServletRequest before HttpServletRequest arrives at the Servlet. Then, you can check HttpServletRequest as needed. You can also modify the HttpServletRequest header and data to intercept HttpServletResponse before reaching the client, check HttpServletResponse as needed. You can also modify the headers and data of HttpServletResponse. For example, this is used when the data sent by the user is filtered and replaced.
Filter usage Basics
1. A Filter must implement the javax. servlet interface and define the following three methods:
Public void init (FilterConfig config)
Public void doFilter (ServletRequest request, ServletResponse response, FilterChain)
Public void destory ()
2. FilterConfig Interface
You can use Configure some initialization parameters for the filter. When the web Container instantiates the Filter object and calls the init method, it will pass in the filterConfig object that encapsulates the filter initialization parameters. Therefore, when writing the filter, using the filterConfig object method, you can get the filter name through getFilterName (), and use String getInitParamerter (String name) to obtain the value of the initialization parameter specified in the deployment description.
3. Use it in the web. xml file And Element to register the written filter class and set the resources it can intercept. The following describes how to configure web. xml by adding a filter during login.
Web. xml
SessionFilter
Com. action. login. SessionFilter
LogonStrings
/Project/index. jsp; login. do
IncludeStrings
. Do;. jsp
RedirectPath
/Index. jsp
Disabletestfilter
N
SessionFilter
/*
FilterServlet
Package com. action. login; import java. io. IOException; import javax. servlet. filter; import javax. servlet. filterChain; import javax. servlet. filterConfig; import javax. servlet. servletException; import javax. servlet. servletRequest; import javax. servlet. servletResponse; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import javax. servlet. http. httpServletResponseWrapper;
Public class SessionFilter implements Filter {public FilterConfig config; public void destroy () {this. config = null;} public static boolean isContains (String container, String [] regx) {boolean result = false; for (int I = 0; I <regx. length; I ++) {if (container. indexOf (regx [I])! =-1) {return true ;}} return result;} public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {HttpServletRequest hrequest = (HttpServletRequest) request; httpServletResponseWrapper wrapper = new HttpServletResponseWrapper (HttpServletResponse) response); String logonStrings = config. getInitParameter ("logonStrings ");
String includeStrings = config. getInitParameter ("includeStrings ");
String redirectPath = hrequest. getContextPath () + config. getInitParameter ("redirectPath ");
String disabletestfilter = config. getInitParameter ("disabletestfilter ");
If (disabletestfilter. toUpperCase (). equals ("Y ")){
Chain. doFilter (request, response); return;} String [] logonList = logonStrings. split (";"); String [] includeList = includeStrings. split (";"); if (! This. isContains (hrequest. getRequestURI (), includeList )){
Chain. doFilter (request, response); return;} if (this. isContains (hrequest. getRequestURI (), logonList )){
Chain. doFilter (request, response); return;} String user = (String) hrequest. getSession (). getAttribute ("useronly ");
If (user = null) {wrapper. sendRedirect (redirectPath); return;} else {chain. doFilter (request, response); return;} public void init (FilterConfig filterConfig) throws ServletException {config = filterConfig ;}}
In this way, the FIlter can help you FIlter.
Conclusion: in the previous article, I have a simple understanding of the Interceptor. Today I have learned the FIlter. In addition to using different methods, there are many differences between them. For example, the filter is based on the callback function, the doFilter method in the filter interface we need to implement is the callback function, while interceptor is based on the reflection mechanism of java itself. This is the essential difference between the two. The filter depends on the servlet container, that is, it can only be executed in the servlet container. Obviously, the doFilter method cannot be called back and forth without the servlet container. Interceptor has nothing to do with servlet containers. In addition, the interceptor is managed by spring and only takes effect on actions. It cannot intercept other resources such as jsp pages and images. The interceptor intercepts user access to the action. If you need to jump to the action, you only need to return a result like the action. spring performs the jump according to the result configuration. If no jump is required, you can call the invocation. invoke (); Method to execute the action requested by the user. The interceptor starts before the action and ends after the action is completed. There must be a lot of different things. I have learned this and I just want to share it with you.