Server review notes (5) filter&Listener

filter

What is filter

Filters in life: water purifiers, air purifiers.
Filters in the web: When accessing server resources, the filter can intercept the request and perform some special functions.

The role of filters

Generally used to complete common operations. Such as: login verification, unified encoding processing, sensitive character filtering...

Filter use

  1. Define a class to implement the Filter interface
  2. Copy method
  3. Configure interception path
    (1) web.xml
    (2) annotation

Filter details

web.xml configuration

Can be implemented with annotations or configured with web.xml


<filter>
        <filter-name>demo01</filter-name>
        <filter-class>com.feitian.web.filter.Filter01Demo</filter-class>
</filter>

<filter-mapping>
        <filter-name>demo01</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

Filter execution process

By default, the dispatcherTypes attribute of the filter is REQUEST, so the execution process of the filter is as follows:
Insert picture description here

Filter life cycle method

  1. init(): After the server is started, the init() method will create a Filter object, and then call the init() method, which is executed only once to load resources.
  2. doFilter(): It always runs during the running of the server, and it will be executed every time a requested resource is intercepted, and executed multiple times.
  3. destroy(): After the server is shut down, the Filter object is destroyed. If the server is shut down normally, the destroy method will be executed. Only execute once. Used to release resources.

Detailed filter configuration

Intercept path configuration

Specific resource path: such as configuration/index.jsp The filter will only be executed when the index.jsp resource is accessed.
Intercept directory: When /user/*accessing all resources under /user, the filter will be executed.
Suffix name interception: When *.jspaccessing all jsp resources, the filter will be executed.
Intercept all resources: When /*accessing all resources, the filter will be executed.

Interception mode configuration

The way the resource is accessed
Annotation configuration: Set the dispatcherTypes attribute

  1. REQUEST: default value
  2. FORWARD: Forward access to resources
  3. INCLUDE: contains access to resources
  4. ERROR: error jump resource
  5. ASYNC: Asynchronous access to resource
    web.xml configuration
<filter-mapping>
        <filter-name>demo01</filter-name>
        <url-pattern>*.jsp</url-pattern>
        <dispatcher>FORWARD</dispatcher>
</filter-mapping>

Filter chain

Execution sequence:

If there are two filters, filter 1 and filter 2
filter 1
filter 2
resource execution
filter 2
filter 1

Problems with filters:
  1. The annotation configuration is compared according to the string rules of the class name, and the smaller value is executed first.
    Such as: AFilter and BFilter, AFilter will be executed first.
  2. web.xml configuration: <filter-mapping>whoever defines the above will execute it first.
<filter>
        <filter-name>demo01</filter-name>
        <filter-class>com.feitian.web.filter.Filter01Demo</filter-class>
</filter>
<filter>
        <filter-name>demo02</filter-name>
        <filter-class>com.feitian.web.filter.Filter02Demo</filter-class>
</filter>

<filter-mapping>
        <filter-name>demo02</filter-name>
        <url-pattern>*.jsp</url-pattern>
        <dispatcher>REQUEST</dispatcher>
</filter-mapping>

<filter-mapping>
        <filter-name>demo01</filter-name>
        <url-pattern>*.jsp</url-pattern>
        <dispatcher>REQUEST</dispatcher>
</filter-mapping>

Results of the:
Insert picture description here

Case

Sensitive word filtering. Some changes are made to the filtering function through design patterns. There are mainly two patterns to enhance the function of the object:

Design Patterns:

Some common ways to solve fixed problems

Decorative pattern
Agency model
concept
  1. Real object: the object being proxied
  2. Agent object:
  3. Proxy mode: proxy objects proxy real objects to achieve the purpose of enhancing the functions of real objects
Method to realize
  1. Static proxy: there is a class file describing the proxy mode
  2. Dynamic proxy: form proxy class in memory

Implementation steps:

  1. The proxy object and the real object implement the same interface
  2. Proxy object = Proxy.newProxyInstance();
  3. Use proxy objects to call methods.
  4. Enhancement method

Enhancement method:

  1. Enhanced parameter list
  2. Enhanced return value type
  3. The method of enhancing body to perform logic
    specific methods can view the case study of a small ape:

Listener

One of the three major components of the web.

Event monitoring mechanism

Event: a thing.
Event source: where the event occurred.
Listener: an object.
Registered listener: bind the event, event source, and listener together. When an event occurs on the event source, execute the listener code

Listener related methods

ServletContextListener: monitor the creation and destruction of ServletContext objects

method:

void contextDestroyed(ServletContextEvent sce): This method will be called before the ServletContext object is destroyed
void contextInitialized(ServletContextEvent sce): This method will be called after the ServletContext object is created

step:

  1. Define a class that implements the ServletContextListener interface
  2. Copy method
  3. Configuration
    1) Use web.xml
<!--注册监听器-->
    <listener>
        <listener-class>com.feitian.web.listener.ContextLoaderListener</listener-class>
    </listener>

    <!--指定初始化参数-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
    </context-param>

2) Notes:

@WebListener
public class ContextLoaderListener implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener
        ......

Guess you like

Origin blog.csdn.net/xueshanfeitian/article/details/110380418