1. The role is to "pre-process and end" the requested resources. (The requested resource can be any url, including JSP, Servlet, and other filters ).
2. class definition method structure, with Code omitted ):
Class MyFilter implements Filter {init () {}// initialize doFilter () {// preprocessing // The requested resource after execution // The End of the requested resource after execution} destroy () {}// destruction}
3. instantiation:
By Tomcat at project startup new, object at startup), and only instantiate once, that is, a project only has one class object.
4. configuration method:
<Filter> <filter-name> name </filter-name> <filter-class> class path </filter-class> </filter> <filter-mapping> <filter- name> name </filter-name> <url-parttern> resource to be accessed <url-parttern> </filter-mapping>
5. method call:
Tomcat automatically calls the request once each time before the requested resource is accessed.
6. Applications in the production environment:
1) text encoding converter (request. setCharacterEncoding ("GBK "))
2) Permission controller (session. getAttribute ("user "))
3) text content filter.