由于Spring Boot默认是以jar包的形式启动嵌入式的Servlet容器来启动Spring Boot的web应用是,没有web.xml配置文件
注册三大组件用以下方式

ServletRegistrationBean
FilterRegistrationBean
ServletListenerRegistrationBean



一、注册一个ServletRegistrationBean

1.定义一个Servlet类

//向页面输出一句hello
public class HttpServlet extends javax.servlet.http.HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("hello");
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}

2.编写一个Servlet配置类

@Configuration
public class MyServerConfig {
//注册三大组件
//注意加上@Bean注解,否者无法加入到容器中
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean servlet = new ServletRegistrationBean(new HttpServlet(), "/myServlet");
return servlet;
}

3.Spring Boot配置文件

server.port=8081
server.servlet.context-path=/boot
server.tomcat.uri-encoding=UTF-8

4.启动主配置文件访问http://localhost:8081/boot/myServlet

这样就生效了



二、注册一个FilterRegistrationBean

1.定义一个Filter类

public class MyFilter 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("success");
filterChain.doFilter(servletRequest,servletResponse);
} @Override
public void destroy() { }
}

2.编写一个filter配置类

@Configuration
public class MyServerConfig {
//注册三大组件 @Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean re = new FilterRegistrationBean();
re.setFilter(new MyFilter());
re.setUrlPatterns(Arrays.asList("/hello","/filter"));
return re;
}

3.启动访问http://localhost:8081/boot/filter


ServletListenerRegistrationBean的使用类似于以上两种组件

Spring Boot 帮我们自动注册了SpringMVC的核心控制器DispatcherServlet,其注册方式类似于以上两种注册

Spring Boot之注册servlet三大组件的更多相关文章

  1. 18. Spring Boot 、注册Servlet三大组件Servlet、Filter、Listener

    由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件 public class MyServlet extends ...

  2. Spring boot中注册Servlet

    Spring boot中注册Servlet 如何在spring boot项目中注册Servlet呢? 如何在spring boot项目中注册Servlet呢? 由于没有web.xml,无法直接在xml ...

  3. SpringBoot 注册Servlet三大组件【Servlet、Filter、Listener】-原生代码+@Bean+效果展示

    由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件. 注册三大组件,class MyServlet()/clas ...

  4. 0017SpringBoot注册Servlet三大组件(Servlet、Filter、Listener)

    由于SpringBoot默认是以jar包的形式启动嵌入式servlet容器来启动SpringBoot的web应用,所以没有web.xml文件,那么如何配置Servlet.Filter.Listener ...

  5. Spring Boot 自定义注册 Servlet、Filter、Listener

    前言 在 Spring Boot 中已经移除了 web.xml 文件,如果需要注册添加 Servlet.Filter.Listener 为 Spring Bean,在 Spring Boot 中有两种 ...

  6. Spring Boot嵌入式的Servlet容器

    一.查看SpringBoot默认的嵌入式Servlet容器(默认使用的是tomcat) 在IDEA的项目的pom文件中按Ctrl + shift + Alt + U可以打开SpringBoot依赖的图 ...

  7. 【串线篇】spring boot配置嵌入式servlet容器

    SpringBoot默认使用Tomcat作为嵌入式的Servlet容器 问题? 一.如何定制和修改Servlet容器的相关配置 1.方法1修改和server有关的配置(ServerProperties ...

  8. Spring boot 梳理 - Spring boot自动注册DispatcherServlet

    spring boot提供的DispatcherServlet的name就是“dispatcherServlet”. 源码 public ServletRegistrationBean dispatc ...

  9. (7)Spring Boot web开发 --- servlet容器

    文章目录 配置嵌入式 Servlet 容器 注册 三大组件 使用其他 servlet 容器 使用外置的 `Servlet` 容器 配置嵌入式 Servlet 容器 Spirng Boot 默认使用自带 ...

随机推荐

  1. Applied Social Network Analysis in Python 相关笔记3

    如果是option2的话,答案选A. 这里节点s,从左边的选择,节点t从右边选择. 这里计算还是用以前的值,不用更新过的值.

  2. 20192204李龙威 2019-2020-2 《Python程序设计》实验三报告

    20192204李龙威 2019-2020-2 <Python程序设计>实验三报告 课程:<Python程序设计> 班级: 1922 姓名: 李龙威 学号:20192204 实 ...

  3. Net Framework 中托管代码与非托管代码的区别

    托管代码与非托管代码的区别 1 简单的说,就是代码被编译成MSIL后在.net的Framework下运行,同操作系统底层的交互都交给framework去做. 所谓非托管代码就是脱离了Framework ...

  4. SQL注入及防止SQL注入

    •SQL注入 SQL注入是通过操作输入来修改事先定义好的SQL语句,对用户输入的字符串进行过滤,转义,限制或处理不严谨,导致用户可以通过输入精心构造的字符串去非法获取到数据库中的数据,以达到执行代码对 ...

  5. Java基础——final、static关键字

    final关键字是最终的意思,可以修饰成员方法.成员变量.类 特点: 1.修饰方法:表示该方法是最终方法,不能被重写 2.修饰变量:表示变量是常量,不能再次被赋值 3.修饰类:表示类是最终类,不能被继 ...

  6. Java基础—private、this关键字及get/set方法

    Java基础-private\this关键字以及get\set方法 1.private关键字 private关键字通常用来修饰成员变量用来保护原有数据的安全,比如在下面学生类中 然后在测试类中调用成员 ...

  7. Mybatis结果集ResultMap映射

    基本使用: 解决属性名和数据库字段名不一致的问题 <resultMap id="user" type="com.guan.bean.UserBean"&g ...

  8. openEuler网络配置+换源+桌面环境ukui等基本环境部署

    镜像下载.域名解析.时间同步请点击阿里云开源镜像站 1.网络配置 你可以选择查看官方文档进行配置:配置网络 (openeuler.org) 接下来的操作基本都需要root权限,所以直接使用root用户 ...

  9. Java如何实现定时任务?

    我是3y,一年CRUD经验用十年的markdown程序员‍常年被誉为优质八股文选手 挺早就规划了要引入分布式定时任务框架了,在年前austin就已经接入了,但代码过年一直都没写,文章也就一直拖到今天了 ...

  10. python3 爬虫 Scrapy库学习1

    1生成项目:生成项目文件夹 scrapy startproject 项目名 2生成爬虫文件 scrapy genspider 爬虫名 指定域名 3进入items文件可以输入自己想要爬取的内容比如 te ...