Filter chain understanding and Filter chain understanding

Source: Internet
Author: User

Filter chain understanding and Filter chain understanding
I. Filter chain

Web. the filter is configured in xml, the init () method is executed during container startup, and the destroy () method is executed to destroy the filter when the container is closed, each time the server receives a request, it will first pass through the filter. If there is a suitable filter, it will execute the doFilter method of the corresponding filter.

The doFilter method has three parameters: ServletRequest, ServletResponse, and FilterChain. The first two parameters are the request and the returned object, which are used for filtering and forwarding. FilterChain is a filter chain that contains all the filters with the same filter conditions, for example:

  

Filters similar to the same matching pattern will exist in the same filter chain, and then sort them sequentially according to the initialization sequence to implement layer-by-layer filtering. The filterChain has a doFilter method. Its function is to forward the current request to the next filter in the filter chain for filtering, and then filter the result, execution can continue only after the next filter is filtered. The execution process is similar:

  

 

For example, the layer-by-layer filtering through the filter chain is like a layer of nesting and a layer of layer. If there is only one filter (or the last one) in the filter chain, the chain is executed. doFilter () directly forwards the request to obtain the request resource. Because the same request and response are transmitted from the same request and response, you can modify the request or return results for each filter, filter and modify.

Code example: (only paste the main code)

ResponseFilter. java

1 public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {2 rows = new rows (HttpServletResponse) servletResponse); 3. out. println ("before ResponseFilter execution"); 4 filterChain. doFilter (servletRequest, customResponseWrapper); // execute the next layer filter 5 System. out. println ("after ResponseFilter execution"); 6} 7}

 

SecondFilter. java

1 public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {2 System. out. println ("before executing Second doFilter"); 3 filterChain. doFilter (servletRequest, servletResponse); // This is the last layer of the filter and will directly request resource4 System. out. println ("after executing Second doFilter"); 5}

Web. xml (the two filters use the same matching pattern '/*', so they will be in the same filter chain)

 1 <filter> 2         <filter-name>ResponseFilter</filter-name> 3         <filter-class>filter.ResponseFilter</filter-class> 4     </filter> 5     <filter-mapping> 6         <filter-name>ResponseFilter</filter-name> 7         <url-pattern>/*</url-pattern> 8     </filter-mapping> 9     <filter>10         <filter-name>SecondFilter</filter-name>11         <filter-class>filter.SecondFilter</filter-class>12     </filter>13     <filter-mapping>14         <filter-name>SecondFilter</filter-name>15         <url-pattern>/*</url-pattern>16     </filter-mapping>

Execution result:

  

The execution sequence of doFilter can be clearly seen from the execution results and the analysis above.

2. Use the Filter to modify the response content

CustomPrintWriter. java (rewrite the PrintWriter method)

1 public class CustomPrintWriter extends PrintWriter {2 private StringBuilder buffer; 3 4 public CustomPrintWriter (Writer out) {5 super (out); 6 buffer = new StringBuilder (); 7} 8 9 @ Override10 public void write (char [] buf, int off, int len) {11 char [] dest = new char [len]; 12 System. arraycopy (buf, off, dest, 0, len); // deeply copy the character array 13 buffer. append (dest); 14} 15 16 public String getContent () {17 return buffer. toString (); 18} 19}

 

CustomResponseWrapper. java (rewrite the HttpServletResponseWrapper method)

 1 public class CustomResponseWrapper extends HttpServletResponseWrapper{ 2     private CustomPrintWriter customPrintWriter; 3  4     public CustomResponseWrapper(HttpServletResponse response) { 5         super(response); 6     } 7  8     @Override 9     public PrintWriter getWriter() throws IOException {10         customPrintWriter = new CustomPrintWriter(super.getWriter());11         return customPrintWriter;12     }13 14     public CustomPrintWriter getCustomPrintWriter() {15         return customPrintWriter;16     }17 }

 

ResponseFilter. java (filter)

1 public class ResponseFilter implements Filter {2 public void init (FilterConfig filterConfig) throws ServletException {} 3 4 public void doFilter (ServletRequest servletRequest, incluservletresponse, FilterChain filterChain) throws IOException, servletException {5 CustomResponseWrapper customResponseWrapper = new CustomResponseWrapper (HttpServletResponse) servletResponse); 6 filterChai N. doFilter (servletRequest, customResponseWrapper); // forward the request to obtain the returned result 7 CustomPrintWriter writer = customResponseWrapper. getCustomPrintWriter (); // obtain the returned result 8 if (writer! = Null) {9 String content = writer. getContent (); 10/** 11 * without modifying the jsp source code, modify the display content 12 */13 content = content. replace ("XXX", LoginCheckServlet. username); 14 servletResponse. getWriter (). write (content); 15} 16} 17 18 public void destroy () {}19}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.