Java 1.3 started with the addition of filter support to the servlet2.3 specification. Filters allow us to intercept requests and responses from target resources.
Some of the filter features:
1. Filter is the servlet specification and requires the support of the servlet container.
2. Filter cannot use resource objects in the Spring framework.
3. Filter works only before and after the servlet.
Filter implementation
We need to implement the method defined in the interface filter:
/* * The contents of this file is subject to the terms * of the Common development and Distribution License * (the "Licen Se "). except * In compliance with the License. * * Can obtain a copy of the license at * glassfish/bootstrap/legal/cddlv1.0.txt or * https://glassfish.dev.java.net/p ublic/cddlv1.0.html. * See the License for the specific language governing * permissions and limitations under the License . * * When distributing Covered Code, include this CDDL * HEADER with each file and include the License file at * Glassfish/bo Otstrap/legal/cddlv1.0.txt. If Applicable, * Add the following below this CDDL HEADER, with the ' fields ' enclosed by brackets ' [] ' replaced with your * Own identifying information:portions copyright [YYYY] * [Name of copyright owner] * * Copyright 2005 Sun Microsystems, Inc. All rights reserved. * * Portions Copyright Apache software Foundation. */package Javax.servlet;import Java.io.ioexception;public interface Filter {public voiD init (Filterconfig filterconfig) throws servletexception; public void DoFilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, Servletex Ception;public void Destroy ();}
Define yourself a Testfilter
/** * You define a filter * @author Zhuli * @date 2014-7-20 */public class Testfilter implements filter {@Override public void init (Filterconfig filterconfig) throws Servletexception {System.out.println ("Testfiltern init");//When the container is initialized, this Side will execute} @Override public void DoFilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, Servletexception {//TODO auto-generated method stub HttpS Ervletrequest request2 = (httpservletrequest) request; String IP = Iputil.getclientip (REQUEST2); System.out.println ("Testfiltern doFilter The request was processed before the + IP: "+ IP"; Chain.dofilter (request, response); Pass the filter chain System.out.println ("After the business has finished processing, it will continue to call this filter, and then call this Side"); } @Override public void Destroy () {//TODO auto-generated Method Stub System.out.println ("Testfiltern Destroy "); When destroying the container. This side will execute. }}
Results:
Testfiltern doFilter request is processed in front of + ip:127.0.0.1
Business logic =========
The filter will continue to be called after the business has been processed. and call this side.
Filter configuration
In Web. Xml. Configure the servlet before configuring filter to:
Url-pattern can configure which requests are required to go this filter
<!--TEST filter--><filter><filter-name>testfilter</filter-name><filter-class> Com.xxx.test.testfilter</filter-class></filter><filter-mapping><filter-name>testfilter </filter-name><url-pattern>/*</url-pattern></filter-mapping>
The order in which the filter is run
Can look at a picture first:
If we have two filters. Testfilter and TestFilter1.
When the Web request comes in
-
We run the code prior to Chain.dofilter in the Testfilter filter (for example, in our sample System.out.println ("Testfiltern doFilter request is handled before");)
-
Then run the code before chain.dofilter in the TestFilter2 filter
-
Then the service method in the servlet
-
Then process the code after chain.dofilter in the TestFilter2 filter
-
Then process the code after the Chain.dofilter in the Testfilter filter (for example in our sample System.out.println ("After the business has finished processing.") will continue to invoke this filter. Then call this Side ");)
Filter life cycle
1. Init initialization. When the Web container is started. Initializes the filter.
Initialization calls the Init () method
2. Filter. Detailed filtering is the Dofilter () method
3. Destroy destroyed. The filter is destroyed when the container is closed.
Java Deep-Filter filters