1.1 Definitions
A filter is a server-side component that intercepts request and response information from the client and filters the information.
1.2 Working principle
1. When the project starts, load the filter from the Web container;
2, the filter exists between the user request and the Web resources;
3, the user request and the Web response between the sending and receiving through the filter filter according to filter rules.
1.3 Life cycle of filters
Instantiation (Web. XML load) → Initialize (init method) → filter (Dofilter method) → Destroy (Destroy method)
1 . Initialize: The init () method is called when the container loads the filter for the first time. The class contains a reference to the Filter Config object in this method. Our filters do not actually need to do this because the initialization information is not used, and this is only for demonstration purposes.
2, Filter: Most of the time the filter is consumed here. The Dofilter method is called by the container, passing in a reference to the servlet request, Servlet Response, and Filter Chain objects that are pointed to in the requests/response chain respectively. The filter then has the opportunity to process the request, pass the processing task to the next resource in the chain (by calling the Dofilter method on the Filter Chain object reference), and then handle the response when processing control returns the filter.
3 . Destroy: The container immediately calls the Destroy () method before garbage collection, so that any necessary cleanup code can be executed.
Filter chain: The server is assembled into a chain in the order defined by the filter in Web. xml
1.4 Basic configuration of the filter in Web. xml
Configuration of the filter in Web. xml:
<Filter><Filter-name> Filter Name</Filter-name><Filter-class> Filter Full class name</Filter-class><Init-param><Description> Description information, can be omitted</Description><Param-name> Parameter name</Param-name><Param-value> Parameter values</Param-value></Init-param></Filter><Filter-mapping> <filter-name< Span style= "COLOR: #0000ff" >> Filter name </filter-name< Span style= "COLOR: #0000ff" >> <url-pattern>url</url-pattern> <dispatcher>[request| include| forward| Error]</dispatcher ></filter-mapping
1.5 Classification of filters
Classification Table of filters
Type |
Role |
REQUEST |
When a user accesses a page directly, the Web container invokes the filter |
FORWARD |
When the target resource is accessed through the RequestDispatcher forward method, the filter is called |
INCLUDE |
When the target resource is called through the include method of RequestDispatcher, the filter is called |
ERROR |
When a target resource is called through a declarative exception handling mechanism, the filter is called |
ASYNC (Servlet3.0 new addition) |
Supports asynchronous processing |
Second, the basic use of the filter
2.1 Design of Filter class
Implement the filter interface, overriding the Init, DoFilter, Destroy methods
PublicClass MyfilterImplementsfilter{Publicvoid init (Filterconfig filterconfig)Throwsservletexception{}/** * make the browser do not cache the filter of the page */public Span style= "COLOR: #0000ff" >void doFilter (servlerrequest request,servletresponse response,filterchain Filterchian) Span style= "COLOR: #0000ff" >throws ioexception,servletexception{( HttpServletResponse) response). SetHeader ("Cache-control", "No-cache" // Pass the processing task to the next resource in the filter chain } Span style= "COLOR: #0000ff" >public void Destroy () {}}
2.2 Registration of filters in Web. xml
1, REQUEST, FORWARD, include and other types of filter registration
<!--Filter configuration-<Filter><Filter-name>myfilter</Filter-name><Filter-class>util.filter.myfilter</Filter-class></Filter><Filter-mapping> <filter-name< Span style= "COLOR: #0000ff" >>myfilter</ Filter-name> < Url-pattern>/main.jsp</ url-pattern> << Span style= "COLOR: #800000" >dispatcher>[request| forward| Include]</dispatcher ></filter-mapping
2. Error Type Filter Registration
<!--Configure error page, jump to/error.jsp page when 404 error occurs-<Error-page><Error-code>404</Error-code><Location>/error.jsp</Location><Error-page><!--Filter configuration-<Filter><Filter-name>errorfilter</Filter-name><Filter-class>util.filter.errorfilter</Filter-class></Filter><Filter-mapping> <filter-name< Span style= "COLOR: #0000ff" >>errorfilter</ Filter-name> < Url-pattern>/error.jsp</ url-pattern> << Span style= "COLOR: #800000" >dispatcher>error</< Span style= "COLOR: #800000" >dispatcher></filter-mapping>
Third, the use of Servlet3.0 filter
Add @WebFilter annotations without registering in Web. XML
@WebFilter (servletnames={"Simpleservlet"},filtername= "Simplefilter")implements filter{}
@WebFilter common property sheet
Property name |
Type |
Describe |
FilterName |
String |
Specify the Name property of the filter, equivalent to <filter-name> |
Value |
String[] |
Equivalent to the Urlpatterns attribute, but not both, and if used at the same time, use the Value property first |
Urlpatterns |
String[] |
Specifies the URL matching pattern for a set of filters, equivalent to the <utl-pattern> tag |
Servletnames |
String[] |
Specifies which servlets the filter will be applied to. Value is the name attribute value in @webservlet, or the <servlet-name> value in Web. xml |
Dispatchertypes |
Dispatchertype |
Specifies the forwarding mode of the filter. Specific values include async, ERROR, FORWARD, include, REQUEST |
InitParams |
Webinitparam[] |
Specifies a set of filter initialization parameters, equivalent to the <init-param> tag |
asyncsupported |
Boolean |
Declares whether the filter supports asynchronous operation mode, equivalent to <async-supported> label |
Description |
String |
The filter description information, equivalent to the <description> label |
DisplayName |
String |
The display name of the filter, equivalent to the <display-name> label |
Java Web Development--filter Filter