Spring Boot integrates Servlet,Filter,Listener, access to static resources

Catalog

Spring Boot integrates Servlet s (two ways)

  1. Create a new maven project

    Create the completed structure diagram:
  2. Introducing a pom.xml dependency
	<!--Introduce parent project-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
    </parent>
    <dependencies>
        <!--SpringBoot web starter-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

First, register the Servlet components by annotation scanning:

  1. Complete registration of Servlet components by annotation scanning
    1.1. Create a Servlet


1.2. Write Servlet code:

@WebServlet(name = "firstServlet",urlPatterns = "/firstServlet") //urlPatterns: Access Path
public class firstServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("In firstServlet");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
    }

1.3. Write startup classes
Create springboot startup class

Code:

@SpringBootApplication
//When spring boot starts, it scans the @WebServlet annotation and creates an instance of the class
@ServletComponentScan
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

Note: The @ServletComponentScan annotation is required on the startup class to mean that the @WebServlet annotation is scanned at startup to create an instance of the Servlet

1.4. Run the startup class and type localhost:8080/firstServlet in the browser

Console Output Information

The second way (by completing registration of Servlet components)

  1. Create a Servlet
  2. Create springboot startup class

    Create a new method to register Servlet components under main method
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    //Add a method with no required method name and must return a ServletRegistrationBean.Register Servlet Objects
    @Bean     //Primary key is equivalent to <bean>label
    public ServletRegistrationBean<SecondServlet> getServletRegistrationBean(){
        ServletRegistrationBean<SecondServlet> bean=
                new ServletRegistrationBean<SecondServlet>(new SecondServlet(),"/SecondServlet");
        return bean;
    }
}
  1. Run startup class and enter localhost:8080/SecondServlet in browser
  2. Console Print Information

Springboot Integrate Filter (similar to Integrate Servlet)

First method (registration of Fliter components via annotation scanning)

  1. Create a new Filter class
  2. Inherit Filter parent class to implement interface

    The code is as follows:
@WebFilter(filterName = "firstFilter", urlPatterns = "/firstFilter")
public class firstFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("----Get into FirstFilter-----");
        chain.doFilter(request, response);//Release
        System.out.println("----leave FirstFilter-----");
    }
}
  1. Create Startup Class

    The code is as follows:
@SpringBootApplication
//When spring boot starts, it scans the @WebServlet @WebFilter @WebListener annotation and creates an instance of the class
@ServletComponentScan
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}

Run the startup class and enter localhost:8080/firstFilter in the browser

404 is reported here because the path after release is not written.

Console Print Information:

Second way (register the Filter component by way of method)

  1. Create a Filter class without writing the @WebFilter annotation
  2. Startup Class

    The code is as follows:
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    //Add a method
    @Bean
    public FilterRegistrationBean<SecondFilter> getFilterRegistrationBean(){
        FilterRegistrationBean<SecondFilter> bean=
                new FilterRegistrationBean<SecondFilter>(new SecondFilter());
        bean.addUrlPatterns("*.do","*.jsp","/SecondFilter"); //Paths ending with.Do,.Jsp, SecondFilter all enter the filter
        return bean;
    }
}

  1. Run startup class and enter localhost:8080/SecondFilter in browser

    Console Print Information:

Springboot Integrated Listener (same as above)

Complete registration of Fliter components by annotation scanning

  1. Create Listener Class


    Listener code:
@WebListener()
public class firstListener implements ServletContextListener{
    //Listen for creation of application objects
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("-----------application objects creating-----------------");
    }
}

  1. Create Startup Class

    Code:
@SpringBootApplication
@ServletComponentScan  //When spring boot starts, it scans the @WebServlet @WebFilter @WebListener annotation and creates an instance of the class
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
  1. Run Startup Class Viewing Console to print information

Second way (by way of completing the Listener component registration)

Omit code like code goes directly to code

  1. Create Listener Class
  2. Startup Class

    Code:
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
    @Bean
    public ServletListenerRegistrationBean<firstListener> getServletListenerRegistrationBean(){
        ServletListenerRegistrationBean<firstListener> bean=
                new ServletListenerRegistrationBean<firstListener>(new firstListener());
        return bean;
    }
}
  1. Run Startup Class Viewing Console to print information

Springboot accesses static resources in two ways

First way (find static resources through the root directory of the ServletContext)

1. Create a directory for webapp under src/main (directory name must be webapp)
Create different directories under webapp to store different static resources, such as images for pictures.

2. Run Startup Class Access Direct Access Resource Path

Second way (find static resources from the directory of classpath/static)

Create a static directory under src/main/resources (the directory name must be static)
Create different directories under static to store different static resources, such as images for pictures.

2. Run Startup Class Access Browser to directly access the resource path

That's all about this tutorial. Thank you for watching it. Please indicate where it came from

Tags: Java SpringBoot Spring JSP Maven

Posted on Tue, 07 Apr 2020 11:45:58 -0700 by oldmanice