Servlet filter methods

Servlet filter methods
A filter is a Java class that implements the javax.servlet.Filter interface. The javax.servlet.Filter interface defines three methods:
serial number Method & Description
1
public void doFilter (ServletRequest, ServletResponse, FilterChain)
This method completes the actual filtering operation. When the client requests a URL that matches the filter settings, the servlet container will first call the filter's doFilter method. FilterChain users access subsequent filters.
2
public void init(FilterConfig filterConfig)
When the web application starts, the web server will create an instance object of Filter, call its init method, read the web.xml configuration, and complete the initialization function of the object, so as to prepare for the interception of subsequent user requests (the filter object only will be created once, and the init method will only be executed once). The developer can obtain the FilterConfig object representing the current filter configuration information through the parameters of the init method.
3
public void destroy()
The servlet container calls this method before destroying the filter instance, in which the resources occupied by the servlet filter are released.


A filter in both JSP and Servlet is a custom Java class that implements the Filter interface .
Function: 1. Filters can dynamically intercept requests and responses to transform or use the information contained in the request or response. ( One or more filters can be attached to a servlet or group of servlets. Filters can also be attached to JavaServer Pages (JSP) files and HTML pages.)
  2. Intercept these requests before the client's request accesses the back-end resources.
  3. Process the responses from the server before sending them back to the client.
Filters are declared via XML tags in the Web deployment descriptor (web.xml), which are then mapped to servlet names or URL patterns in your application's deployment descriptor.
When the web container starts the web application, it creates an instance for each filter you declare in the deployment descriptor.
The execution order of Filter is consistent with the configuration order in the web.xml configuration file. Generally, Filter is configured before all Servlets.

Filter Filter life cycle


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324490213&siteId=291194637