SpringBoot2.x系列教程39--整合Servlets, Filters和listeners

SpringBoot2.x系列教程39--整合Servlets, Filters和listeners

作者:一一哥

一. Spring Boot中Servlets,Filters和Listeners

1. 概述

在Spring Boot中使用内嵌servlet容器时,我们可以通过使用Spring beans或扫描Servlet组件的方式注册Servlets,Filters及特定Servlet相关的所有listeners(比如HttpSessionListener)。

Spring Boot 提供了 ServletRegistrationBean, FilterRegistrationBean, ServletListenerRegistrationBean 三个类分别用来注册 Servlet, Filter, Listener。

示例代码:

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 一一哥
 */
public class RegisterServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = getServletConfig().getInitParameter("name");
        String sex = getServletConfig().getInitParameter("sex");

        resp.getOutputStream().println("name is " + name);
        resp.getOutputStream().println("sex is " + sex);
    }

}

@Bean
public ServletRegistrationBean registerServlet() {
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
            new RegisterServlet(), "/registerServlet");
    servletRegistrationBean.addInitParameter("name", "一一哥");
    servletRegistrationBean.addInitParameter("sex", "man");
    return servletRegistrationBean;
}

2. Servlet 3.0组件扫描注册

Servlet 3.0 之前,Servlet、Filter、Listener 这些组件都需要在 web.xml 中进行配置,3.0 之后开始不再需要 web.xml 这个配置文件了,所有的组件都可以通过代码配置或者注解来达到目的。

Servlet 3.0后提供了3个注解来代替:

  • @WebServlet代替 servlet 配置;
  • @WebFilter代替 filter 配置;
  • @WebListener代替 listener 配置

二. Spring Boot中Servlet的使用

接下来我带大家讲解一下Spring Boot中实现Servlet的开发,主要是讲解两种实现方式,组件注册方式和注解实现方式。

我们首先创建一个Spring boot的web项目,添加web的相关依赖包,具体过程略。

1. 组件注册方式

1.1 创建UserServlet类

package com.yyg.boot.web;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/3/29
 * @Description Description
 */
@Slf4j
public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        log.warn("第一种Servlet实现方式-->组件注册方式");
        resp.getWriter().write("success");
    }

}

1.2 注册Servlet组件

我们可以在启动类Application或者别的配置类中,利用ServletRegistrationBean来注册刚才创建的Servlet组件。

package com.yyg.boot;

import com.yyg.boot.web.UserServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/3/29
 * @Description Description
 */
@SpringBootApplication
public class ServletApplication {

    public static void main(String[] args){
        SpringApplication.run(ServletApplication.class,args);
    }

    /**
     *ServletRegistrationBean:注册创建的Servlet类
     */
    @Bean
    public ServletRegistrationBean getServletRegistrationBean(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new UserServlet());
        //url映射路径
        bean.addUrlMappings("/user");
        return bean;
    }

}

1.3 启动程序进行测试

我们启动程序,在浏览器输入
http://localhost:8080/user
可以看到控制台中成功输出如下信息。

浏览器中显示如下内容:

2. 注解实现方式

2.1 创建OrderServlet类

创建OrderServlet类,在该类上添加@WebServlet注解。

package com.yyg.boot.web;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/3/29
 * @Description 第二种实现Servlet的方式, 通过@WebServlet注解来实现注册.
 */
@Slf4j
@WebServlet(name = "OrderServlet", urlPatterns = "/order")
public class OrderServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        log.warn("第2种Servlet实现方式-->注解实现方式");
        resp.getWriter().write("success");
    }

}

2.2 添加@ServletComponentScan注解

注意,如果使用的是 Spring Boot 内嵌服务器,需要在配置类上面添加额外的 @ServletComponentScan 注解来开启 Servlet 组件扫描功能,如果使用的是独立的服务器,则不需要添加,会使用服务器内部的自动发现机制。

package com.yyg.boot;

import com.yyg.boot.web.UserServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/3/29
 * @Description Description
 */
@SpringBootApplication
@ServletComponentScan(basePackages = "com.yyg.boot.web")
public class ServletApplication {

    public static void main(String[] args){
        SpringApplication.run(ServletApplication.class,args);
    }
}

2.3 启动程序进行测试

我们启动程序,在浏览器输入
http://localhost:8080/user
可以看到控制台中成功输出如下信息:

浏览器中显示如下内容:

三. Filter与Listener的使用

Spring Boot中Filter、Listener的使用,与Servlet的用法类似,在此不进行一一讲解,感兴趣的朋友可以自行尝试。




 

发布了315 篇原创文章 · 获赞 84 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/syc000666/article/details/105181321