I. Concept:
Filter, also known as filters, is a more exciting technology in Servlet technology, Web developers through the filter technology, Web server management of all Web resources: such as JSP, Servlet, static picture files or static HTML files, etc. to intercept, So that some special functions can be realized. For example, the implementation of URL-level access control, filtering sensitive words, compressed response information and other advanced features.
Second, Filter introduction
The Servlet API provides a filter interface that, when developing a Web application, if the Java class is written to implement this interface, the Java class is called filter filter. Filter technology enables developers to intercept requests and responses to access before they access a target resource. Simply put, it is possible to implement a Web container for the pre-access interception of a resource before processing it, or to intercept a resource before it returns a response to the Web container.
Three. Code examples
<1> Basic Demo
Package Cn.edu.hpu.lesson;
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;
1. Create a new class to implement the filter interface
public class Filterlesson1 implements Filter {
public void Destroy () {
}
2, implement the Dofilter () method, print a word, to prove the ability to intercept
public void DoFilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
System.out.println ("Before call first filter.");
Chain.dofilter (Request,response);
System.out.println ("After call first filter.");
}
public void init (Filterconfig arg0) throws Servletexception {
}
}
Add the following in Web. xml
3. Configuring in Web. XML (refer to servlet configuration)
4. Finally open the Web test to get the following results
Summary:
For the application of filter I believe we have understood that it is the main function of the user to access a target resource before the request and response to the access to intercept, do some processing, and then invoke the target program, the advantage is that some common operations can be abstracted, take the setting character set, If this is not the case, we will write a statement that sets the character set for each page. Not only is trouble and maintenance difficult, but if you use filter, you just need to add a class, in the XML configuration, if you do not want to use, the contents of the configuration file can be removed.
In fact, this is an AOP (Aspect orientedprogramming), which is oriented towards tangent programming. Its main intent is to divide the code of logging, performance statistics, security control, transaction processing, exception handling, and so forth from the business logic code, and by separating these behaviors, we want to be able to separate them into non-instructional methods of business logic, and then change these behaviors without affecting the code of the business logic.
For the setting character set, it is not the content of the business logic, for the processing of these things we can extract, using the filter for the overall setting, which is equivalent to the content of the class is further abstracted, so that our system more flexible, more responsive to change!
Java Web Learning Filter (1)