On the Struts2 filter

     filter:

           Filter can handle subscriber requests and response procedures, it can be used to access control, transcoding applications. Filters are part of the servlet specification, any java web project can be used.

    Before using the filter you have to define a filter (instantiate an object), you can also write your own based on the needs of a class (inherited java.Servlet.Filter interface, three methods init, doFilter and destory) then here is instantiated, for the following use.

. 1  < filter > 
2          < filter-name > Filter Name </ filter-name > 
. 3          < filter-class > filter corresponding class </ filter-class > 
. 4          <-! Initialization parameters -> 
. 5          < INIT- param >     
. 6              < param-name > parameter </ param-name > 
. 7              < param-value > parameters </ param-value > 
. 8          </ the init-param > 
. 9  </filter>

 

    Filters must be associated with a specific URL can play a role, there are three ways associated :( filter name is the above-defined filters)

             1 (only when requested by filtration xxx.jsp) associated with a resource URL

1 <filter-mapping>
2         <filter-name>过滤器名</filter>
3         <url-pattern>xxx.jsp</url-pattern>
4 </filter-mapping>

    2. associated with all of the resources of a URL directory (any request will be filter)

1 <filter-mapping>
2         <filter-name>过滤器名</filter>
3         <url-pattern>/*</url-pattern>
4 </filter-mapping>

    3. (Servlet filters the request specified) associated with a Servlet

. 1  < filter-Mapping > 
2          < filter-name > name of the filter </ filter > 
. 3          < URL-pattern > the Servlet Name </ URL-pattern > 
. 4  </ filter-Mapping >

   Thus, a simple Struts2 configured to filter the (above configuration are carried out in web.xml). But its implementation process is kind of how it?

           Suppose now that there is a client request (assuming that the request has been associated), the request goes through a series of filters (the filter chain), are each passed through a filter of an object class instance corresponding to the current filter according to the ( call the init method), and calls inside doFilter method (the method is put inside your own filtering logic), only through the current filter will filter to the next, until it passes all filters, if not by the transferred to the specified error page . (The following is a filter when the code is executed when a filter by, in the process doFilter)

1  // this is a normal request, let it pass through
2     chain.doFilter(request, response);

 

Then call FilterDispatchar, processed differently depending on the user's request ..... (not content filter, it is not described)

 

Guess you like

Origin www.cnblogs.com/baikaizhuliangshui/p/11621235.html