Java Web development-Filter filter, Filter filter
I. Filter
1.1 Definition
A filter is a server component that intercepts user-side request and Response Information and filters the information.
1.2 Working Principle
1. Load the filter from the Web container when the project is started;
2. filters exist between user requests and Web resources;
3. Sending and receiving between user requests and Web responses are filtered by filters according to filter rules.
1.3 lifecycle of a filter
Instantiation (web. xml loading) → initialization (init method) → filtering (doFilter method) → destruction (destroy method)
1. Initialization: The init () method is called when the container loads the filter for the first time. This class contains a reference pointing to the Filter Config object in this method. Our filter does not actually need to do this, because initialization information is not used here, just for demonstration purposes.
2. filter: most of the time consumed by the filter is here. The doFilter method is called by the container and is passed in to reference Servlet Request, Servlet Response, and Filter Chain objects in the Request/Response Chain respectively. The Filter then has the opportunity to process the request and pass the processing task to the next resource in the Chain (by calling the doFilter method referenced by the Filter Chain object ), then, the response is processed when the control is returned to the filter.
3. destroy: The container immediately calls the destroy () method before garbage collection to execute any necessary cleanup code.
Filter chain: the server assembles a chain according to the sequence defined by the filters in web. xml.
1.4 basic configuration of filters in web. xml
Configuration of the filter in web. xml:
<Filter> <filter-name> filter name </filter-name> <filter-class> complete filter class name </filter-class> <init-param> <description> description information, </description> <param-name> parameter name </param-name> <param-value> parameter value </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 filter Classification
Filter category table
Type |
Function |
REQUEST |
When you access the page directly, the web Container will call this filter. |
FORWARD |
When the target resource is accessed through the RequestDispatcher's forward method, the filter will be called |
INCLUDE |
When the target resource is called through the include method of RequestDispatcher, the filter will be called. |
ERROR |
When the target resource is called through the declarative Exception Handling Mechanism, the filter will be called. |
ASYNC (New in Servlet3.0) |
Support asynchronous Processing |
Ii. basic use of filters
2.1 design of the filter class
Implement the Filter interface and rewrite the init, doFilter, and destroy methods.
Public class MyFilter implements Filter {public void init (FilterConfig filterConfig) throws ServletException {}/*** disable the browser from caching the page Filter */public void doFilter (ServlerRequest request, ServletResponse response, filterChain filterChian) throws IOException, ServletException {(HttpServletResponse) response ). setHeader ("Cache-Control", "no-cache"); (HttpServletResponse) response ). setHeader ("Pragma", "no-cache"); (HttpServletResponse) response ). setDateHeader ("Expires",-1); filterChain. doFilter (request, response); // pass the processing task to the next resource in the filter chain} public void destroy (){}}
2.2 register a filter in web. xml
1. Registration of filter types such as REQUEST, FORWARD, and INCLUDE
<! -- Filter configuration --> <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
<! -- Configuration error page. When a 404 error occurs, it is redirected to/error. jsp page --> <error-page> <error-code> 404 </error-code> <location>/error. jsp </location> <error-page> <! -- Filter configuration --> <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>
3. Use of filters in Servlet3.0
Add the @ WebFilter annotation without registering it in web. xml.
@WebFilter(servletNames={"SimpleServlet"},filterName="SimpleFilter")public class MyFirstFilter implements Filter{}
@ WebFilter common Attribute Table
Attribute name |
Type |
Description |
FilterName |
String |
Specify the name attribute of the filter, which is equivalent to <filter-name> |
Value |
String [] |
It is equivalent to the urlPatterns attribute, but the two are not used at the same time. If both are used, the value attribute is used first. |
UrlPatterns |
String [] |
Specifies the URL matching mode for a set of filters, which is equivalent to the <utl-pattern> label. |
ServletNames |
String [] |
Specify the Servlet to which the filter applies. The value is the name attribute value in @ WebServlet, or the <servlet-name> value in web. xml. |
DispatcherTypes |
DispatcherType |
Specify the forwarding mode of the filter. Specific values include async, ERROR, FORWARD, INCLUDE, REQUEST |
InitParams |
WebInitParam [] |
Specify a set of filter initialization parameters, which is equivalent to the <init-param> label. |
AsyncSupported |
Boolean |
Whether the declared filter supports asynchronous operation mode is equivalent to the <async-supported> label. |
Description |
String |
The description of the filter, which is equivalent to the <description> tag. |
DisplayName |
String |
The display name of the filter, which is equivalent to the <display-name> label. |