+ Filter plus 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, do 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 all resources before, the filter will be executed
                        public class FilterDemo1 the implements the filter {
                            @Override
                            public void init(FilterConfig filterConfig) throws ServletException {
                       
                            }
                       
                            @Override
                            public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
                                System.out.println("filterDemo1被执行了....");
                       
                       
                                //放行
                                filterChain.doFilter(servletRequest,servletResponse);
                       
                            }
                       
                            @Override
                            public void destroy() {
                       
                            }
                        }


        3. Filter details:
                1. Configuring the web.xml       
                        <filter>
                        <name-filter> the demo1 </ name-filter>
                        <-filter class> cn.itcast.web.filter.FilterDemo1 </-filter class>
                    </ filter>
                    <-filter Mapping>
                        <-filter name> the demo1 </ name-filter>
                                <-! intercept path ->
                        <URL-pattern> / * </ URL pattern->
                    </ Mapping filter->
                2. filter performs process
                        1. Run the filter
                        2.After the release of execution resources
                        3. Filter release the code execution back below code
                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. Perform multiple
                        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. Detailed filter configuration
                        * 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, filters 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 will be implemented
                        * 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> tag i.e. may be
                               
                5. The filter chain (a plurality of filters)
                        * execution order: if there are two filters: filter 12 and filters
                                1 1. Filter
                                2. Filter 2
                                3. Resource executed
                                4. 2 filter
                                5. The filter 1

                        * Filter problem order:
                                1. Configuration Annotations: comparison string comparison rules in accordance with the class name, the first value is smaller execution
                                        * such as: 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, then jump to the login page, it says "You are not logged in, please login."
               
       

                2. Case 2_ filtering sensitive words
                        * demand:
                                1. day17_case cases entry of data in sensitive words filtered
                                2. Reference sensitive words "sensitive words .txt"
                                3. If you are sensitive vocabulary, replaced with ***

                        * Analysis:
                                a for enhanced request object. Enhancing acquisition parameters related methods
                                2. release. Passing proxy object


                        function * Enhanced object:
                                * Design Patterns: solve some common problems fixed manner
                                1. decorative pattern
                                2. Proxy mode
                                        * concept:
                                                1. real object: object being proxied
                                                2. proxy object:
                                                3. Agent mode: Proxy proxy object real object, the purpose of enhancing the real object function
                                         * implementation:
                                                 1. Static Agent: There is a class file description proxy mode
                                                 2. dynamic agent: agent class is formed in the memory
                                                        * implementation steps:
                                                                1. proxy object and the real object implement the same interface
                                                                2. the proxy object the Proxy.newProxyInstance = ();
                                                                3. the method of using the proxy object call.
                                                                4. A method of enhancing

                                                        * Enhanced mode:
                                                                1. Enhance the list of parameters
                                                                2. Return Type enhancement
                                                                logic 3. The method of enhancing the bank execution       


Listener ##: Listener
        * concept: one of the three web component.
                * 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, listeners execute the code


        * ServletContextListener: listening ServletContext object is created and destroyed
                * Methods:
                        * void contextDestroyed (ServletContextEvent SCE): calls ServletContext object is destroyed before the method
                        * void contextInitialized (ServletContextEvent sce): This method will be called after the object is created ServletContext
                * steps:
                        1. Define a class that implement the interface ServletContextListener
                        2. replication method
                        configuration
                                1. the web.xml
                                                <listener>
                                              <listener-class> cn.itcast.web.listener.ContextLoaderListener </ listener-class>
                                                   </ listener>

                                                * Specify initialization parameters <context-param>
                                2. Annotation:
                                        * @WebListener
Published 966 original articles · won praise 11 · views 30000 +

Guess you like

Origin blog.csdn.net/xiaoyaGrace/article/details/105270553