码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-06-13 23:31:34      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:doget   inf   mamicode   etc   add   new   一个   方法   java   

Spring Boot中整合ServletFilterListener的方式

写在前面

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

一、Spring Boot中整合Servlet的方式

(一)整合Servlet方式一

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

技术图片

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

可以看到控制台输出

技术图片

 

(二)整合Servlet方式二

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

 技术图片

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组件的注册

技术图片

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中整合Filter的方式

(一)整合Filter方式一

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

技术图片

 

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控制台输出

技术图片

 

(二)整合Filter方式二

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

技术图片

 

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类,修改代码;

技术图片

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中整合Listener的方式

(一)整合Listener方式一

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

技术图片

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()方法,控制台输出

技术图片

 

(二)整合Listener方式二

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

技术图片

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类,修改代码:

技术图片

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的方式)

标签:doget   inf   mamicode   etc   add   new   一个   方法   java   

原文地址:https://www.cnblogs.com/gaolight/p/13121984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!