Spring Boot 2 is configured using Servlet, Listener, and Filter

Keywords: Java Session IntelliJ IDEA SpringBoot

Development Environment: IntelliJ IDEA 2019.2.2
Spring Boot Version: 2.1.8

Create a new SpringBoot project named demo.

1. Configuring with Servlet s

1. Modify the code for the startup class DemoApplication.cs and add the annotation ServletComponentScan, which scans for Servlet components, including using @WebServlet,
Classes decorated with @WebFilter and @WebListener.

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

2. Create a new MyServlet.cs class, inherit HttpServlet and add the comment @WebServlet

package com.example.demo;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(value="/servlet")
public class MyServlet extends HttpServlet {
    public MyServlet(){
        System.out.println("servlet class");
    }
    protected void service(HttpServletRequest arg0, HttpServletResponse arg1){
        System.out.println("servlet Method");
    }
}

Visit http://localhost:8080/servlet in browser and see IDEA console output
servlet class
servlet method

2. Use Listener Configuration

1. The code for the startup class DemoApplication.cs has added the annotation ServletComponentScan to the configuration using Servlets, which remains unchanged here.

2. Create a new class MyServlet.cs

package com.example.demo;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class MyListener implements ServletRequestListener {
    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent){
        System.out.println("Request Creation");
    }
    @Override
    public void requestDestroyed(ServletRequestEvent servletRequestEvent){
        System.out.println("Request Destroy");
    }
}

Access an interface in the browser, such as http://localhost:8080/servlet, to see the IDEA console output:
Request Creation
Request Destroy

Attached, commonly used listener interfaces:

1.ServletContextListener -- Listens for creation and destruction of servletContext objects 
    1.1 contextInitialized (ServletContextEvent arg0) -- executed at creation time 
    1.2 contextDestroyed (ServletContextEvent arg0) -- Executed when destroyed
2.HttpSessionListener -- listens for session object creation and destruction 
    2.2 session Created (HttpSessionEvent) -- Executed at creation time 
    2.2 session Destroyed (HttpSessionEvent) -- Executed on Destruction
3.ServletRequestListener -- Listens for the creation and destruction of request objects 
    3.1 requestInitialized (ServletRequestEvent sre) -- executed at creation time 
    3.2 requestDestroyed (ServletRequestEvent sre) -- Executed at destruction time
4.ServletContextAttributeListener -- Listens for changes in attributes in the servletContext object 
    4.1 AttributeAdded (ServletContextAttributeEvent event) -- Executed when adding attributes 
    4.2 attributeReplaced (ServletContextAttributeEvent event) -- Executed when attributes are modified 
    4.3 attributeRemoved (ServletContextAttributeEvent event) -- Executed when attributes are deleted
5.HttpSessionAttributeListener -- listens for property changes in session objects 
    5.1 AttributeAdded (HttpSessionBindingEvent event) -- Executed when adding attributes 
    5.2 attributeReplaced (HttpSessionBindingEvent event) -- Executed when modifying attributes 
    5.3 attributeRemoved (HttpSessionBindingEvent event) -- Executed when attributes are deleted
6.ServletRequestAttributeListener -- listens for property changes in request objects 
    6.1 AttributeAdded (ServletRequestAttributeEvent srae) -- Executed when adding attributes 
    6.2 attributeReplaced (ServletRequestAttributeEvent srae) -- Executed when attributes are modified 
    6.3 attributeRemoved (ServletRequestAttributeEvent srae) -- Executed when attributes are deleted

3. Configuration with Filter

1. The code for the startup class DemoApplication.cs has added the annotation ServletComponentScan to the configuration using Servlets, which remains unchanged here.

2. Create a new class MyFilter.cs

package com.example.demo;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

//The first parameter is the filter name, and the second parameter is the request address to intercept
@WebFilter(filterName="myFilter", urlPatterns="/*")
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("filter Initialization");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, 

ServletException {
        System.out.println("filter Method");
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        System.out.println("filter Destroy");
    }
}

Visit http://localhost:8080/servlet in the browser and see the IDEA console output:
filter method

Finally, the project structure diagram is attached:

Posted by Awanka on Tue, 24 Sep 2019 09:59:45 -0700