由于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. CF1534F1题解

    首先,对于一个在第 \(i\) 行 \(j\) 列的沙子,如果他开始下降,他能够使哪些沙子下降呢? 很容易得知是第 \(j-1,j,j+1\) 列所有行号不小于 \(i\) 的沙子. 对于沙子 \(u ...

  2. SP3734题解

    题意: 有 \(n\) 列表格,第 \(i\) 列有 \(a_i\) 个格子,问在 \(n\) 列表格中有多少种放置 \(k\) 个棋子的方法使没有棋子在同一列和同一行.(如果中间有一个"格 ...

  3. 详解 Java 内部类

    内部类在 Java 里面算是非常常见的一个功能了,在日常开发中我们肯定多多少少都用过,这里总结一下关于 Java 中内部类的相关知识点和一些使用内部类时需要注意的点. 从种类上说,内部类可以分为四类: ...

  4. Java基础——ArrayList

    Java基础--ArrayList 作用:提供一个可变长度的集合,底层实际上是一个可变长度的数组 格式:ArrayList <E> arr=new ArrayList<>(); ...

  5. .net为程序集签名之.pfx文件

    项目中误删了.pfx证书文件,导致项目无法启动. 以为很快就能在网上找到解决方案,应该没关系,不过找了半个小时,都没有有效的解决办法,搜出来很多.pfx文件是一个证书文件,里面存储公钥和私钥,对于我要 ...

  6. wordpress在线检测主题和插件

    http://wpthemedetector.coderschool.cn/ http://www.wpthemedetector.com/ http://whatwpthemeisthat.com ...

  7. linux下安装简单的文件上传与下载工具 lrzsz

    编译安装 1.从下面的网站下载 lrzsz-1.12.20.tar.gz wget https://ohse.de/uwe/releases/lrzsz-0.12.20.tar.gz 2.查看里面的I ...

  8. FusionCube 9000 虚拟化之数据库场景(虚拟化超融合基础设施)

    1.本场景为fusioncube9000虚拟化场景,带两台dbn节点. 配置清单: 1台cna节点 2台dbn节点,部署数据库操作系统,一般安装oracle数据双机. 2台mcna节点,作为管理节点, ...

  9. 羽夏看Win系统内核——结语

    写在前面   此系列是本人一个字一个字码出来的,包括示例和实验截图.由于系统内核的复杂性,故可能有错误或者不全面的地方,如有错误,欢迎批评指正,本教程将会长期更新. 如有好的建议,欢迎反馈.码字不易, ...

  10. z-index原理及适用范围

    z-index原理及适用范围 原理 z-index这个属性控制着元素在z轴上的表现形式,堆叠顺序是当前元素位于z轴上的值,数值越大说明元素的堆叠1顺序越高,越靠近屏幕. 适用范围 <div cl ...