Spring Boot integration Servlet, Filter, Listener, access static resources

Spring Boot integration Servlet (two ways)

  1. Create a new maven project
    Here Insert Picture Description
    to create a structure diagram after completion:
    Here Insert Picture Description
  2. Pom.xml dependent on the introduction of
	<!--引入父项目-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
    </parent>
    <dependencies>
        <!--SpringBoot web启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

The first way (to complete the registration Servlet component by annotating scan mode):

  1. Complete Servlet component by way of annotations scanning registered
    1.1. Creating a Servlet

Here Insert Picture Description
1.2 Servlet write the code:

@WebServlet(name = "firstServlet",urlPatterns = "/firstServlet") //urlPatterns:访问路径
public class firstServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进来了firstServlet");
    }

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

1.3 start writing class
to create springboot startup class
Here Insert Picture Description
Code:

@SpringBootApplication
//在spring boot启动时会扫描@WebServlet注解,并创建该类的实例
@ServletComponentScan
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

Note: On startup class need to add @ServletComponentScan comment means: when you start scanning instance @WebServlet notes, create a Servlet

1.4 running start classes in your browser and enter localhost: 8080 firstServlet /
Here Insert Picture Description
console output
Here Insert Picture Description

The second way (by means of Servlet components to complete registration)

  1. Creating a Servlet
    Here Insert Picture Description
  2. Creating springboot classes start
    Here Insert Picture Description
    the new method in the main components of a registered Servlet method
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    //添加一个方法,方法名无要求,必须返回ServletRegistrationBean。注册Servlet对象
    @Bean     //主键等价于<bean>标签
    public ServletRegistrationBean<SecondServlet> getServletRegistrationBean(){
        ServletRegistrationBean<SecondServlet> bean=
                new ServletRegistrationBean<SecondServlet>(new SecondServlet(),"/SecondServlet");
        return bean;
    }
}
  1. Run startup class in your browser type localhost: 8080 / SecondServlet
    Here Insert Picture Description
  2. Print Console Information
    Here Insert Picture Description

Springboot Integration Filter (Servlet and integrated manner similar)

The first embodiment (to complete the registration Fliter annotation component by scanning)

  1. Create a Filter class
    Here Insert Picture Description
  2. Filter inherit parent class implements the interface
    Here Insert Picture Description
    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("----进入FirstFilter-----");
        chain.doFilter(request, response);//放行
        System.out.println("----离开FirstFilter-----");
    }
}
  1. Create a startup class

    code is as follows:
@SpringBootApplication
//在spring boot启动时会扫描@WebServlet @WebFilter @WebListener注解,并创建该类的实例
@ServletComponentScan
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}

Running start classes in your browser and enter localhost: 8080 / firstFilter
Here Insert Picture Description
here because the newspaper did not write path 404 after the release;

Print Console information:
Here Insert Picture Description

The second way (to complete the registration Filter component by ways and means)

  1. Create a Filter class without writing notes @WebFilter
    Here Insert Picture Description
  2. Start the class
    Here Insert Picture Description
    code is as follows:
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    //添加一个方法
    @Bean
    public FilterRegistrationBean<SecondFilter> getFilterRegistrationBean(){
        FilterRegistrationBean<SecondFilter> bean=
                new FilterRegistrationBean<SecondFilter>(new SecondFilter());
        bean.addUrlPatterns("*.do","*.jsp","/SecondFilter"); //以.do , .jsp , SecondFilter结尾路径的都会进到过滤器
        return bean;
    }
}

  1. Run startup class in your browser type localhost: 8080 / SecondFilter
    Here Insert Picture Description
    console print information:
    Here Insert Picture Description

Springboot integration Listener (ibid)

Fliter complete assembly by scanning Register notes

  1. Create a class Listener
    Here Insert Picture Description
    Here Insert Picture Description
    Listener Code:
@WebListener()
public class firstListener implements ServletContextListener{
    //监听application对象的创建
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("-----------application对象创建-----------------");
    }
}

  1. Create a startup class
    Here Insert Picture Description
    Code:
@SpringBootApplication
@ServletComponentScan  //在spring boot启动时会扫描@WebServlet @WebFilter @WebListener注解,并创建该类的实例
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
  1. Run startup class to see the console print info
    Here Insert Picture Description

The second way (by the method completes Listener component registration)

As will be omitted code directly on the code Code

  1. Create a Listener class
  2. Start Class
    Here Insert Picture Description
    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 to see the console print info
    Here Insert Picture Description

Springboot access static resources (two ways)

The first method (static resources by looking at the root of the ServletContext)

1. Create in src / main directory of a webapp (directory name must be a webapp)
to create a different directory in the webapp to store different static resources, such as: images put pictures.
Here Insert Picture Description
2. Run to start direct access to resources like access path
Here Insert Picture Description
Here Insert Picture Description

The second way (look for static resources from the directory classpath / static's)

Create a static directory under src / main / resources (directory name must be static)
create different directories stored in static different static resources, such as:. Images put pictures
Here Insert Picture Description
2. Run to start the class browser to directly access resources access path
Here Insert Picture Description
Here Insert Picture Description

These are related to the contents of this tutorial, thanks for watching, please indicate the source

Guess you like

Origin www.cnblogs.com/joker-dj/p/12657528.html