Relations and differences between Java filters and Spring MVC interceptors, Java listeners

Keywords: Java xml Apache Web Server

1. Java filters:

1. In Java Web, the incoming request and response filter out some information in advance, set some parameters in advance, set the character set uniformly, control whether to log in, and then process it in the incoming Servlet.

2. Filter Chain: In a Web application, you can write multiple filters, which together are called a Filter Chain. The Web server decides which Filter to call first according to the order in which Filter is registered in the web.xml file.

3. When the doFilter method of the first Filter is called, the web server creates a FilterChain object representing the Filter chain to pass to the method. In the doFilter method, if the developer calls the doFilter method of the FilterChain object, the web server checks whether there is a filter in the FilterChain object, if there is one, calls the second filter, and if not, calls the target resource.

Writing examples of filter s in Java Web

2. Spring MVC interceptor:

1. Interceptor is face-to-face programming, which relies on Java's dynamic proxy technology. (Interceptors are wrapped in filters)

2. Interceptor stack: It is to link the interceptors into a chain in a certain order.
When accessing the intercepted method or field, the interceptor in the interceptor chain is called in the order it was previously defined (xml).

The difference between filter and interceptor:

1. The interceptor is based on the reflection mechanism of java, and the filter is based on function callback.
2. The filter depends on the Servlet container, while the interceptor does not depend on the Servlet container.
3. Interceptors can only work on action requests, while filters can work on almost all requests (e.g. js,.css, etc.).
4. Interceptors can access objects in action context and value stack, but filters cannot.
5. In the action lifecycle, the interceptor can be called many times, while the filter can only be called once when the container is initialized.
6. The interceptor can get the bean s in the IOC container, but the filter can't. This is very important. A service is injected into the interceptor to invoke the business logic.

Remarks:
** action:** Action class is the bridge between user request and business logic, and is the Controller layer in MVC mode.

Execution order: before filtering -> before intercepting -> action processing -> after intercepting -> after filtering
Filtration is a horizontal process:
1. Firstly, the content submitted by the client is filtered (e.g. the processing that the unregistered user can not access the internal page).
2. After the filtering is passed, the interceptor will check the validation of the data submitted by the user and do some pre-processing of the data.
3. Then the processed data is sent to the corresponding Action.
4. After the Action process is completed and returned, the interceptor can also do other processes.
5. Finally, it returns up to the subsequent operation of the filter.

Note: Introduction in related e-books

IV. Java listener (turn):

1. Servlet Listener is a server-side program that implements the javax.servlet.ServletContextListener interface. It also starts with the start of web application, initializes only once, and destroys with the stop of web application.
2. The main function is to do some initial content addition work, set some basic content (such as some parameters or some fixed objects, etc.).

Java code:

package com.lzw.filter.demo;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class InitDataListener implements ServletContextListener {

    private static ServletContext servletContext;

    public static ServletContext getServletContext() {
        return servletContext;
    }

    @Override
    public void contextInitialized(ServletContextEvent contextEvent) {
        servletContext = contextEvent.getServletContext();
        //final ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        System.out.println("Server startup finished!");
        System.out.println(servletContext);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

   <listener>
        <listener-class>com.lzw.filter.demo.InitDataListener</listener-class>
   </listener>
</web-app>

Console results:

information: Starting service Catalina
2013-10-31 15:13:55 org.apache.catalina.core.StandardEngine startInternal
//Information: Starting Servlet Engine: Apache Tomcat/7.0.42
//Server startup finished!
org.apache.catalina.core.ApplicationContextFacade@7966340c
2013-10-31 15:13:56 org.apache.coyote.AbstractProtocol start
//Information: Starting ProtocolHandler["http-apr-8080"]
2013-10-31 15:13:56 org.apache.coyote.AbstractProtocol start
//Information: Starting ProtocolHandler["ajp-apr-8009"]
2013-10-31 15:13:56 org.apache.catalina.startup.Catalina start
//Information: Server startup in 402 ms

Posted by drbigfresh on Fri, 28 Jun 2019 16:53:19 -0700