在Spring Boot中使用Servlet,根据Servlet注册方式的不同,有两种使用方式。若使用的是Servlet3.0+版本,则两种方式均可使用;若使用的是Servlet2.5版本,则只能使用配置类方式

一、Servlet3.0+版本方式

(1)创建工程07-servlet

(2)导入依赖


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.5.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.abc</groupId>
  12. <artifactId>07-servlet</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <properties>
  15. <java.version>1.8</java.version>
  16. </properties>
  17. <dependencies>
  18. <!--热部署依赖-->
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-devtools</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-test</artifactId>
  30. <scope>test</scope>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <plugins>
  35. <plugin>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-maven-plugin</artifactId>
  38. </plugin>
  39. </plugins>
  40. </build>
  41. </project>

(3)创建Servlet

(4)定义Filter

(5) 修改入口类

在入口类中添加Servlet扫描注解

(6) 测试

http://localhost:8080/some

二、Servlet2.5版本方式

(1)创建工程07-servlet2

复制07-servlet,命名07-servlet2

(2)导入依赖

无需修改

(3) 定义Servlet

(4)定义Filter

(5) 定义配置类

(6) 修改入口类

(7)测试

http://localhost:8080/some

Spring Boot中使用Servlet与Filter的更多相关文章

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

    在Spring Boot中整合Servlet.Filter.Listener的方式 写在前面 从零开始的Spring Boot(1.搭建一个Spring Boot项目Hello World):http ...

  2. Spring boot中使用servlet filter

    Spring boot中使用servlet filter liuyuhang原创,未经允许请勿转载! 在web项目中经常需要一些场景,如参数过滤防止sql注入,防止页面攻击,空参数矫正等, 也可以做成 ...

  3. Spring boot中注册Servlet

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

  4. spring boot中使用servlet、listener和filter

    spring boot中支持使用java Web三大组件(servlet.listener和filter),但是坑比较多,主要是spring boot内嵌tomcat和独立tomcat服务器有一些细节 ...

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

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

  6. spring boot 中使用servlet

  7. Spring Boot中一个Servlet主动断开连接的方法

    主动断开连接,从而返回结果给客户端,并且能够继续执行剩余代码. 对于一个HttpServletResponse类型的对象response来说,执行如下代码: response.getWriter(). ...

  8. 传统的Servlet在spring boot中怎么实现的?

    传统的Servlet在spring boot中怎么实现的? 本文主要内容: 1:springboot一些介绍 2:传统的servlete项目在spring boot项目中怎么实现的?web.xml.u ...

  9. SpringBoot中使用Servlet,Filter,Listener

    项目最近在替换之前陈旧的框架,改用SpringBoot进行重构,初接触,暂时还没有用到Servlet,Filter,Listener的地方,但在之前回顾Servlet的生命周期时,https://ww ...

随机推荐

  1. sm2加密

    import java.math.BigInteger; import java.security.NoSuchAlgorithmException; import java.security.Sec ...

  2. Spark整合Hive

    spark-sql 写代码方式 1.idea里面将代码编写好打包上传到集群中运行,上线使用 spark-submit提交 2.spark shell (repl) 里面使用sqlContext 测试使 ...

  3. MyBatis 中为什么不建议使用 where 1=1?

    最近接手了一个老项目,"愉悦的心情"自然无以言表,做开发的朋友都懂,这里就不多说了,都是泪... ​ 接手老项目,自然是要先熟悉一下业务代码,然而在翻阅 mapper 文件时,发现 ...

  4. Windows应用开发中程序窗口中的各种图标尺寸规划

    为了让你的图标在各个视图模式下都能有合适的尺寸,需要制作4种尺寸16x16.32x32.48x48.256x256 在Windows系统中,几乎所有窗口都是ListView,其中的图标都按照指定的尺寸 ...

  5. 唯一id生成器

    public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1");//id自增 Long ...

  6. Go语言核心36讲(Go语言实战与应用十五)--学习笔记

    37 | strings包与字符串操作 Go 语言不但拥有可以独立代表 Unicode 字符的类型rune,而且还有可以对字符串值进行 Unicode 字符拆分的for语句. 除此之外,标准库中的un ...

  7. 论文翻译:2021_Decoupling magnitude and phase optimization with a two-stage deep network

    论文地址:两阶段深度网络的解耦幅度和相位优化 论文代码: 引用格式:Li A, Liu W, Luo X, et al. ICASSP 2021 deep noise suppression chal ...

  8. 洛谷 P4135 作诗(分块)

    题目链接 题意:\(n\) 个数,每个数都在 \([1,c]\) 中,\(m\) 次询问,每次问在 \([l,r]\) 中有多少个数出现偶数次.强制在线. \(1 \leq n,m,c \leq 10 ...

  9. P5509 派遣

    题面传送门. 数论小杂烩( 由题意,对于每个士兵 \(i\),要么选,对答案产生 \(a_i(\frac{x}{i-x})\) 倍的贡献,要么不选,对答案产生 \(1\) 倍的贡献. 由此可知每个士兵 ...

  10. azkaban执行任务长时间无法结束

    问题显示: 由于一次执行较多的任务,导致azkaban的web程序崩溃,此时,关闭azkaban服务,重新启动azkaban 但是由于azkaban的exec程序无法关闭,这里采用kill的方式关掉e ...