Filter filter arrangement

Recommended: Servlet Filter explain in detail the

With a plurality of filter intercepted request, sequentially arranged filter by web.xml

From the final filter configuration response returns the starting of filtration

The configuration procedure:

Write Filter implementation class

 1 package cn.kihyou.b2c.filter;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.Filter;
 6 import javax.servlet.FilterChain;
 7 import javax.servlet.FilterConfig;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11 
12 //实现接口:javax.servlet.Filter;
13 public class AdminFilter implements Filter {
14 
15      // three important ways: Initialization: the init (); intercept method to execute: the doFilter (); destruction: the destroy (); 
16  
. 17      @Override
 18 is      public  void the init (the FilterConfig FilterConfig) throws ServletException {
 . 19          // the TODO Method Stub-Generated Auto
 20 is          // Filter.super.init (FilterConfig); 
21 is      }
 22 is  
23 is      @Override
 24      public  void the doFilter (the ServletRequest Request, the ServletResponse Response, the FilterChain catena alberghiera)
 25              throws IOException, {ServletException
 26 is          // the TODO Auto-Generated Stub Method,
 27         // 1. interception client / on a filter incoming requests, whether to release to the Servlet / next the Filter 
28          System.out.println ( "AdminFilter intercepts the request" );
 29          // release (release You can not put yourself figure) 
30          the chain.doFilter (Request, response);
 31 is          // 2. knockdown servlet / response back to the filter, a filter whether to release the client / a 
32          System.out.println ( "AdminFilter intercepted response" );
 33 is  
34 is      }
 35  
36      @Override
 37 [      public  void the destroy () {
 38 is          // the TODO Auto-Generated Method Stub
 39          // Filter.super.destroy (); 
40      }
 41 is 
42 }

 

Configuring web.xml

. 1  <-! The Filter, Filter -> 
2      < filter > 
. 3          <-! Filter name, easily play -> 
. 4          < filter-name > AdminFilter </ filter-name > 
. 5          <-! Implementation class where directory -> 
. 6          < filter-class > cn.kihyou.b2c.filter.AdminFilter </ filter-class > 
. 7          <! - initialization parameters -> 
8          <! - character set, charset: UTF-8 - -> 
. 9          < the init-param > 
10              <!- Parameter name-> 
. 11              < param-name > charset </ param-name > 
12 is              <-! Parameter values -> 
13 is              < param-value > UTF-. 8 </ param-value > 
14          </ the init-param > 
15  
16          <! - set the content type and character set, contentType: text / HTML; charset = UTF-. 8 -> 
. 17          < the init-param > 
18 is              < param-name > contentType </ param-name > 
. 19              < param-value >text / HTML; charset = UTF-. 8 </ param-value > 
20 is          </ the init-param > 
21 is      </ filter > 
22 is  
23 is      <! - filter mapping -> 
24      < filter-Mapping > 
25          ! <- name corresponding filter -> 
26 is          < filter-name > AdminFilter </ filter-name > 
27          <-! to intercept directory filter -> 
28          < URL-pattern > / Web / ADMIN / * </ url- pattern > 
29      </Mapping-filter > 
30  
31 is      < the session-config > 
32          <-! timeout, unit: minute -> 
33 is          < the session-timeout > 30 </ the session-timeout > 
34 is      </ the session-config >

 

Guess you like

Origin www.cnblogs.com/kihyou/p/11594171.html