Introduction of Filter
Filter, also known as filters, is one of the most exciting technologies in servlet technology, Web developers through the filter technology, to the Web server management of all Web resources: such as JSP,
Servlet, static picture files or static HTML files to intercept, so as to achieve some special functions. such as the implementation of URL-level access control, filtering sensitive words, compressed response information, etc.
Some advanced features.
The Servlet API provides a filter interface that, when developing a Web application, is called filter filter if the Java class that is written implements the interface. Through filter
Technology, developers can implement user access to a target resource before the request and response to access to intercept, filter interface source code:
[Java]View plain copy public abstract interface filter{public abstract void init (Filterconfig paramfilterconfig) throw s servletexception; public abstract void Dofilter (ServletRequest paramservletrequest, Servletresponse paramservletresponse, FilterChain Paramfilterchain) throws IOException, servletexception; public abstract void Destroy (); }
Second, how the filter is to achieve interception. (The working principle of filter)
There is a Dofilter method in the filter interface, and when we write the filter and configure which Web resource to intercept, the Web server each time before invoking the service method of the Web resource,
Will first call the Dofilter method of the filter, so writing code within the method can do the following:
Let a piece of code execute before calling the target resource.
Whether to call the target resource (that is, whether to let the user access the Web resource).
After the target resource is invoked, let a piece of code execute.
When the Web server calls the Dofilter method, it passes a Filterchain object, which is the most important object in the filter interface, and also provides a
Dofilter method, the developer can decide whether or not to call this method, and call the method, the Web server will invoke the service method of the Web resource, that is, the Web resource will be accessed.
Otherwise, the Web resource will not be accessed.
Third, the filter development process
3.1. Filter development steps
Filter development is divided into 2 steps:
* Write Java class to implement the filter interface and implement its Dofilter method.
* Use the <filter> and <filter-mapping> elements in the Web.xml file to register the written filter class and set the resources it can intercept.
Filter Example:
[Java] View Plain copy 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; /** * @author yangcq * @description Filters Filter Filter Working principle */ public class filtertest implements filter{ public void destroy () { system.out.println ("----filter Destroyed----"); } public void dofilter ( Servletrequest request, servletresponse response,filterchain filterchain) throws ioexception, servletexception { // Some preprocessing for request and response Request.setcharacterencoding ("UTF-8"); Response.setcharacterencoding ("UTF-8");