由于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. npm vue路由配置

    npm vue路由 复习:1.README.md文件:保存如何使用的命令 (1)     npm install:拷项目时可以不拷node_modules文件,运行该命令时,会自动下载node_mod ...

  2. C++设计模式 - 组合模式(Composite)

    数据结构模式 常常有一-些组件在内部具有特定的数据结构,如果让客户程序依赖这些特定的数据结构,将极大地破坏组件的复用.这时候,将这些特定数据结构封装在内部,在外部提供统一的接口,来实现与特定数据结构无 ...

  3. .net Core 调用EF Core 爬坑

    .net Core 中调用 EF Core项目 首先得建一个类库供.net Core 的主项目引用:其次,在.net Core 中做注入:最后,在页面中调用 1.类库项目中的操作 1.新建类库项目 2 ...

  4. Div+CSS 定位 Position

    position 属性规定应用于元素的定位方法的类型(static.relative.fixed.absolute 或 sticky). position: static;HTML 元素默认情况下的定 ...

  5. kubernetes集群节点多网卡,calico指定网卡

    kubernetes集群节点多网卡,calico指定网卡 1.calico如果有节点是多网卡,所以需要在配置文件中指定内网网卡 spec: containers: - env: - name: DAT ...

  6. oracle 中有数据但是sql查询不出来结果(中文)

    如 select * from user where name like '%王%': 无数据: 而数据库中确实有姓王的用户. 配置环境变量 NLS_LANG = AMERICAN_AMERICA.A ...

  7. 使用tc ingress来限速接收方向

    Linux中的QoS分为入口(Ingress)部分和出口(Egress)部分,入口部分主要用于进行入口流量限速(policing),出口部分主要用于队列调度(queuing scheduling).大 ...

  8. [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业

    [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业 bzoj   bzoj 题目大意:一个序列,m个询问在$[l,r]$区间的$[x,y]$范围内的数的个数/种类. ...

  9. CF1504X Codeforces Round #712

    CF1504D Flip the Cards(找规律+贪心) 题目大意:给你n张牌,正反面都有数字,保证所有牌上的数字在$[1,2n]$内且互不相同.你可以翻转任意张牌,接下来需要把牌按正面的数字从小 ...

  10. chrome删除保存的密码

    chrome删除保存的密码 关于谷歌密码管理器 该管理器是将我们的密码管理在google的账号中,当然,谷歌是说用了加密技术保存的,不会存储明文. https://passwords.google.c ...