Today’s content

1. Filter: Filter 2. Listener: ListenerCopy the code

-Leonard: Filter.

1. Concepts: * Filters in life: water purifiers, air purifiers, bandits, * Filters on the Web: Filters can intercept requests when accessing server resources to accomplish some special functions. * Filters are used to perform general operations. Such as: login verification, unified coding processing, sensitive character filtering... 1. Define a class to implement interface Filter 2. Copy method 3. Configure the interception path 1. web. XML 2. Code: @webfilter ("/*") Public class FilterDemo1 implements 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 is executed...." ); / / release filterChain doFilter (servletRequest, servletResponse); } @Override public void destroy() { } } 3. Filter details: 1. Web. XML configuration < filter > < filter - name > not < / filter - name > < filter - class > cn. Itcast. Web. Filter. FilterDemo1 < / filter - class > </filter> <filter-mapping> <filter-name>demo1</filter-name> <! - intercept path - > < url - the pattern > / * < / url - the pattern > < / filter - mapping > 2. Filter execution flow 1. Filter execution 2. Resources after being released 3. Filter lifecycle method 1. init: After the server is started, a Filter object is created and init is called. Perform this operation only once. Used to load resources. 2. DoFilter: Executed each time a request is made for an intercepted resource. 3. Destroy: After the server is shut down, the Filter object is destroyed. If the server is properly shut down, the destroy method is executed. Perform this operation only once. Filter configuration Details * Intercept path configuration: 1. The filter is executed only when the index. JSP resource is accessed. Blocking directory: /user/* All resources under /user are accessed by the filter 3. Postname blocking: *.jsp accesses all resources with postname JSP, and the filter is executed. Intercepting all resources: /* Filters are executed when all resources are accessed * Intercepting Mode Configuration: How resources are accessed * Notes Configuration: * Set the dispatcherTypes property. 1. REQUEST: default. Browser requests resources directly 2. FORWARD: forwards resources 3. INCLUDE: access resources 4. <dispatcher></dispatcher> </dispatcher> Filter chain (configure multiple filters) * Execution order: If there are two filters: Filter 1 and filter 2 1. Filter 1 2. Filter 2 3. Resource Execution 4. Filter 2 5. Filter 1 * Filter order problem: 1. Annotation configuration: according to the string comparison rules for class names, the small value of the first to execute * such as: AFilter and BFilter, AFilter is executed first. 2. Web. XML configuration: <filter-mapping> Who is defined in the above? Case: 1. Case 1_ Login Verification * Requirement: 1. Access the resources of day17_case. Verify the login. 2. If yes, permit the login. 3. If you have not logged in, the login page is displayed, prompting "You have not logged in, please log in first". 2. Case 2_ Sensitive word Filtering * Requirements: 1. Sensitive word filtering for the data entered in day17_case 2. For sensitive words, see sensitive Words.txt. 3. For sensitive words, replace them with *** *. Enhance the Request object. Enhanced methods for obtaining parameters 2. Release. Pass proxy objects * Enhance object functionality: * Design Patterns: Some common ways to solve fixed problems 1. Decorative mode 2. Proxy mode * Concept: 1. Real object: the object being propped up 2. Proxy object: 3. Proxy mode: Proxy object Proxies real objects to enhance the function of real objects * Implementation mode: 1. Static proxy: there is a class file describing the proxy mode. 2. Dynamic proxy: Form the proxy class in memory. 2. Proxy object = proxy.newProxyInstance (); 3. Invoke methods using proxy objects. Enhancement method * Enhancement method: 1. Enhancement parameter list 2. Enhancement return value type 3. Enhance method body execution logicCopy the code

Listener: indicates a Listener

* Concept: One of the three major components of the Web. * Event listener mechanism * Event: an event * Event source: where the event occurs * Listener: an object * Register listener: binds the event, event source, and listener together. When an event occurs on the event source, execute the listener code * ServletContextListener: Listen for the creation and destruction of the ServletContext object * methods: * void contextDestroyed(ServletContextEvent sCE) : * void contextInitialized(ServletContextEvent sce) : This method is called after the ServletContext object is created * Steps: 1. Define a class that implements the ServletContextListener interface 2. Manifold method (3) configuration 1. Web. XML < listener > < listener - class > cn. Itcast. Web. Listener. ContextLoaderListener < / listener - class > </listener> * Specify the initialization parameter <context-param> 2. Note: * @webListenerCopy the code