Detailed description of the Filter in JavaWeb Servlet, javawebservlet
 
Detailed description of Filter in Java Web Servlet
 
1. Brief Introduction
 
Filter: filters all web resources on the web server to implement some special functions (permission access control, filtering sensitive words, and compressing response information ).Filters can check and modify the requests and responses of Servlet containers. They cannot generate request and response, and only provide filtering function.(Before the Servlet is called, check the Request object to modify its related information. After the Servlet is called, check the Response to modify its related information.) The Filter object is resident on the server.
 
2. Lifecycle
 
(1) Start the server, read the configuration file web. xml, load the Filter class and instantiate it, and call init ();
(2). the user-accessed resource is matched by the url-pattern of the Filter, and doFilter () is called. The object to be filtered is delivered to the next Filter through the chain. doFilter () method;
(3) If the application is stopped or reloaded, call destroy ().
 
3. chain filter chain
 
 
The two filters have different filtering functions. The server will assemble a chain according to the order defined by the filters in web. xml, and execute the doFilter () of each filter in sequence through chain. doFilter (). Interruption can be performed at any time. As long as chain. doFilter () is not executed, the following filters and request content will not be executed.
 
4. filter configuration
 
(1) inherit the Filter interface and rewrite the doFilter () method
(2). Configure web. xml to filter the operations that meet the requirements
 
About url-pattern configuration>
 
/** MyFilter. java * filter */package com. baidu. web; import java. io. IOException; import javax. servlet. filter; import javax. servlet. filterChain; import javax. servlet. filterConfig; import javax. servlet. servletException; import javax. servlet. servletRequest; import javax. servlet. servletResponse; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class MyFilter implements Filter {@ Override public void destroy () {}@ Override public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, servletException {// process the request HttpServletResponse resp = (HttpServletResponse) response; HttpServletRequest req = (HttpServletRequest) request; String username = (String) req. getSession (). getAttribute ("username"); if (req. getRequestURI (). contains ("Buy") | req. getRequestURI (). contains ("collect") {if (username = null | username. equals ("") {// meets filtering requirements and processes req. getRequestDispatcher ("/login. jsp "). forward (req, resp); return;} chain. doFilter (req, resp); // process the corresponding response} @ Override public void init (FilterConfig filterConfig) throws ServletException {// filterConfig, through getServletContext () of filterConfig () method To obtain ServletContext }} 
<filter> <filter-name>filter</filter-name> <filter-class>com.baidu.web.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
 
If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!