从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

在Spring Boot中整合Servlet、Filter、Listener的方式

写在前面

从零开始的Spring Boot(1、搭建一个Spring Boot项目Hello World):https://www.cnblogs.com/gaolight/p/13097818.html

从零开始的Spring Boot(3、Spring Boot静态资源和文件上传):https://www.cnblogs.com/gaolight/p/13130406.html

一、在Spring Boot中整合Servlet的方式

(一)整合Servlet方式一

1.如图创建Servlet包,FirstServlet类;

从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

2. 修改FirstServlet内的代码,如下;

package com.demo.springbootweb.Servlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="FirstServlet",urlPatterns = "/first")
public class FirstServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)  {
        System.out.println("FirstServlet...");
    }
}

3.为启动类xxxxApplication.java增加注解@ServletComponentScan

package com.demo.springbootweb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan//spring boot启动时扫描注解@WebServlet,并将该类实例化
public class SpringbootwebApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootwebApplication.class, args);
    }
}

4.运行启动类main方法,在浏览器中输入地址:http://localhost:8080/first

可以看到控制台输出

从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

(二)整合Servlet方式二

1.创建SecondServlet并修改代码如下:

从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

package com.demo.springbootweb.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
public class SecondServlet extends HttpServlet{
    public void doGet(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("SecondServlet...");
    }
}

3. 创建config包和ServletConfig类,并修改代码;

该代码也可以写在启动类下,完成Servlet组件的注册

从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

package com.demo.springbootweb.config;
import com.demo.springbootweb.Servlet.SecondServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.Servlet;
@Configuration
public class ServletConfig {
    @Bean
    public ServletRegistrationBean getServletRegistrationBean(){
        ServletRegistrationBean bean=new ServletRegistrationBean((Servlet) new SecondServlet());
        bean.addUrlMappings("/second");
        return bean;
    }
}

4. 运行启动类main方法,在浏览器输入:http://localhost:8080/second

可以看到控制台输出

从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

二、在Spring Boot中整合Filter的方式

(一)整合Filter方式一

1. 在filter包下创建FirstFilter类,并修改代码;

从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

package com.demo.springbootweb.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(filterName="FirstFilter",urlPatterns = "/first")
//@WebFilter(filterName="FirstFilter",urlPatterns = {"*.do","*.jsp"})
public class FirstFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入FirstFilter");
        filterChain.doFilter(servletRequest,servletResponse );
        System.out.println("离开FirstFilter");
    }
    @Override
    public void destroy() {
    }
}

2. 之前在启动类中添加的@ServletComponentScan,同样可以扫描注解@WebFilter

package com.demo.springbootweb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan//spring boot启动时扫描注解@WebServlet,@WebFilter,并将该类实例化
public class SpringbootwebApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootwebApplication.class, args);
    }
}

3. 运行启动类main方法,在浏览器中输入http://localhost:8080/first,控制台输出

从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

(二)整合Filter方式二

1.如图创建SecondFilter类,并修改代码;

从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

package com.demo.springbootweb.filter;
import javax.servlet.*;
import java.io.IOException;
public class SecondFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入SecondFilter");
        filterChain.doFilter(servletRequest,servletResponse );
        System.out.println("离开SecondFilter");
    }
    @Override
    public void destroy() {
    }
}

2. 为启动类添加注解;

package com.demo.springbootweb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan//spring boot启动时扫描注解@WebServlet,@WebFilter,并将该类实例化
public class SpringbootwebApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootwebApplication.class, args);
    }
}

3. 创建FilterConfig类,修改代码;

从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

package com.demo.springbootweb.config;
import com.demo.springbootweb.filter.SecondFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FilterConfig {
    @Bean
    public FilterRegistrationBean getFilterRegistrationBean(){
        FilterRegistrationBean bean=new FilterRegistrationBean(new SecondFilter());
        //bean.addUrlPatterns(new  String[]{"*.do","*.jsp"});
        bean.addUrlPatterns("/second");
        return bean;
    }
}

4. 运行启动类main方法,在浏览器中输入:http://localhost:8080/second

从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

三、在Spring Boot中整合Listener的方式

(一)整合Listener方式一

1.创建FirstListener类,并修改代码

从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

package com.demo.springbootweb.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class FirstListener implements ServletContextListener {
    @Override
    public void contextDestroyed(ServletContextEvent event) {
    }
    @Override
    public void contextInitialized(ServletContextEvent event) {
        System.out.println("FirstListener....init...");
    }
}

2. 为启动类添加注解

package com.demo.springbootweb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan//spring boot启动时扫描注解@WebServlet,@WebFilter,@WebListener并将该类实例化
public class SpringbootwebApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootwebApplication.class, args);
    }
}

3. 运行启动类main()方法,控制台输出

从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

(二)整合Listener方式二

1.创建SecondListener类,修改代码:

从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

package com.demo.springbootweb.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class SecondListener implements ServletContextListener {
    @Override
    public void contextDestroyed(ServletContextEvent event) {
    }
    @Override
    public void contextInitialized(ServletContextEvent event) {
        System.out.println("SecondListener....init...");
    }
}

2.创建ListenerConfig类,修改代码:

从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

package com.demo.springbootweb.config;
import com.demo.springbootweb.listener.SecondListener;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ListenerConfig {
    @Bean
    public ServletListenerRegistrationBean getServletListenerRegistrationBean(){
        ServletListenerRegistrationBean bean=new ServletListenerRegistrationBean(new SecondListener());
        return bean;
    }
}

4. 运行启动类main()方法,控制台输出:

从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

上一篇:大新闻!HoloLens即将入华商用


下一篇:PHP中的include、include_once、require、require_once