Filter+Listener

# Filter: Filter
 1. Concept:
  * Filter life: water purifiers, air purifiers, bandits,
  * the Web filter: When resource access server, the request filter can intercept them, complete some special features.
  * The role filter:
   * perform common operations generally used. Such as: login authentication, unified coding process, sensitive character filtering ...
 2. Quick Start:
  1. Step:
   1. definition of a class that implements the interface to the Filter
   2. The method of replication
   Configuration knockdown path
    . 1 the web.xml.
    2. Annotations
  2. Code:
   @WebFilter ( "/ *") // Access prior to all resources, the filter will be executed
   public class FilterDemo1 the implements the filter {
       @Override
       public void the init (the FilterConfig FilterConfig) throws ServletException {
   
       }
   
       @Override
       public void the doFilter (the servletRequest servletRequest, the ServletResponse ServletResponse, the filterChain filterChain) throws IOException, {ServletException
           the System. out.println ( "filterDemo1 been executed ....");
   
   
           // release
            filterChain.doFilter(servletRequest,servletResponse);
   
       }
   
       @Override
       public void destroy() {
   
       }
   }

 3. Filter details:
  1. the web.xml Configuration 
   <filter>
          <filter-name> the demo1 </ filter-name>
          <filter-class> cn.itcast.web.filter.FilterDemo1 </ filter-class>
      </ filter >
      <Mapping-filter>
          <filter-name> the demo1 </ filter-name>
    <-! knockdown path ->
          <URL-pattern> / * </ URL-pattern>
      </ filter-Mapping>
  2. filter implementation process
   1. perform filter
   2. perform resources after release
   3. back through the code to release the code below the filter
  3. the filter life cycle approach
   1. init: after the server is started, it will create a filter object, and then call the init method . Performed only once. For loading resources
   2. doFilter: every resource request is intercepted, it will perform. Executed multiple times
   3. destroy: After the server is shut down, Filter object is destroyed. If the server is shut down properly, it will perform the destroy method. Performed only once. For releasing resources
  4. Filter Configuration Explanation
   * intercept path configuration:
    1. Specific resource path: /index.jsp index.jsp only access the resource, the filter will be executed
    2. intercepted directory: / User / access * / when all of the resources of the user, the filter will be executed
    3. intercept extension: * .jsp suffix named jsp access to all resources, the filter will be executed
    4. intercept all resources: / * to access all resources, filters It will be executed
   * Block mode configuration: how resources are being accessed
    * Notes configuration:
      * set dispatcherTypes property
      1. REQUEST: default. Direct browser request resources
      2. FORWARD: forward access resources
      3. INCLUDE: contains access resource
      4. ERROR: Error Skip resources
      5. ASYNC: asynchronous access resources
    * the web.xml configuration
     * Set <dispatcher> </ dispatcher> tags i.e. may be
    
  5 filter chain (a plurality of filters).
   * execution order: If there are two filters: filter 1 and filter 2
    1 1. Filter
    2. Filter 2
    3. Resource perform
    4. 2 filter
    5. The filter 1
   * Filter issue order:
    1. Configuration Annotations: comparison string comparison rules in accordance with the class name, the first value is smaller performed
     * such as: the AFILTER and BFilter, AFilter on the first executed.
    2. web.xml configuration: <filter-mapping> Who defined in the top, the first to perform
  4. Case:
  1. Case 1_ login authentication
   * Requirements:
    1. resource access day17_case cases. Verify that you are logged
    2. If you are logged in, the direct release.
    3. If you do not sign in, go to the login page, it says "You are not logged in, please login."
   
 
  2. Case sensitive words filtered 2_
   * Requirements:
    1. day17_case cases entered data sensitive words filtered
    2. sensitive words refer to "sensitive words .txt"
    3. If you are sensitive vocabulary, replaced ***
   * Analysis:
    1. request object is enhanced. Enhancing acquisition parameters related methods
    2. release. Delivery Agent objects

   * Enhanced Object features:
    * Design Patterns: solve some common problems fixed manner
     1. decorative pattern
    2. Proxy mode
     * concepts:
      1. The real object: The object being proxied
      2. The proxy object:
      3. Agent mode: proxy object agent real object, the real purpose of enhancing the function of the object
      * ways:
       1. static agent: agent describes a class file mode
       2. dynamic agent: agent class is formed in the memory
       * implementation steps:
        1. the agent object and the real object implementation the same interface
        2. the proxy object the Proxy.newProxyInstance = ();
        3. the method of using the proxy object call.
        4. Enhancement
       * Enhanced mode:
        1. Enhance the list of parameters
        2. Return Value enhancement type
        3. Enhancement logic executor 

## Listener: Listener
 * Concept: One of the three web components.
  * Event listener mechanism
   * Event: One thing
   * Event Source: Local events
   * Listener: An object
   * registered listeners: event, event source, listeners are bound together. When an event occurs on the event source, the listener code execution

 * ServletContextListener: listening ServletContext object creation and destruction
  * Methods:
   * void contextDestroyed (ServletContextEvent SCE): This method is called before ServletContext object is destroyed
   * void contextInitialized (ServletContextEvent sce): This method is called after ServletContext object is created
  * Step:
   1 define a class that implements an interface ServletContextListener
   2. The method of replication
   configuration
    1. the web.xml
      <listener>
           <listener-class> cn.itcast.web.listener.ContextLoaderListener </ listener-class>
         </ listener>
      * Initialization parameters specified <context-param>
    2. Annotation:
     * @WebListener
 

Guess you like

Origin www.cnblogs.com/ironman-yan/p/12092489.html