First, filter
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, you can omit</Description> <Param-name>Parameter name</Param-name> <Param-value>Parameter values</Param-value> </Init-param></Filter><filter-mapping> <Filter-name>Filter Name</Filter-name> <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
Public classMyfilterImplementsfilter{ Public voidInit (Filterconfig filterconfig)throwsservletexception{}/*** Make the browser not cache the filter of the page*/ Public voidDoFilter (servlerrequest request,servletresponse response,filterchain Filterchian)throwsioexception,servletexception{((httpservletresponse) response). SetHeader ("Cache-control", "No-cache"); ((httpservletresponse) response). SetHeader ("Pragma", "No-cache"); ((httpservletresponse) response). Setdateheader ("Expires",-1); Filterchain.dofilter (request, response);//pass a processing task to the next resource in the filter chain } Public voiddestroy () {}}
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>Myfilter</Filter-name> <Url-pattern>/main.jsp</Url-pattern> <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>Errorfilter</Filter-name> <Url-pattern>/error.jsp</Url-pattern> <Dispatcher>ERROR</Dispatcher></filter-mapping>
Third, the use of Servlet3.0 filter
Add @WebFilter annotations without registering in Web. xml
@WebFilter (servletnames={"Simpleservlet"},filtername= "Simplefilter")publicclass 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 |
Reference: MU Lesson Net "JAVA Web Development Technology Application-filter" http://www.imooc.com/learn/213
Java Web Development--filter Filter