Spring Boot使用Servlet及Filter過程詳解
在Spring Boot中使用Servlet,根據(jù)Servlet注冊方式的不同,有兩種使用方式。若使用的是Servlet3.0+版本,則兩種方式均可使用;若使用的是Servlet2.5版本,則只能使用配置類方式
一、Servlet3.0+版本方式
(1)創(chuàng)建工程07-servlet

(2)導(dǎo)入依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.abc</groupId>
<artifactId>07-servlet</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--熱部署依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(3)創(chuàng)建Servlet

(4)定義Filter

(5) 修改入口類
在入口類中添加Servlet掃描注解

(6) 測試
http://localhost:8080/some

二、Servlet2.5版本方式
(1)創(chuàng)建工程07-servlet2
復(fù)制07-servlet,命名07-servlet2
(2)導(dǎo)入依賴
無需修改
(3) 定義Servlet

(4)定義Filter

(5)定義配置類

(6) 修改入口類

(7)測試
http://localhost:8080/some

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 淺談SpringMVC的攔截器(Interceptor)和Servlet 的過濾器(Filter)的區(qū)別與聯(lián)系 及SpringMVC 的配置文件
- JavaWeb Servlet中Filter過濾器的詳解
- servlet過濾器(Filter)詳解(九)
- 詳解Servlet之過濾器(Filter)
- springboot掃描自定義的servlet和filter代碼詳解
- SpringBoot初始教程之Servlet、Filter、Listener配置詳解
- 詳談Servlet和Filter的區(qū)別以及兩者在Struts2和Springmvc中的應(yīng)用
- web.xml中servlet, bean, filter, listenr 加載順序_動力節(jié)點Java學(xué)院整理
- Filter、Servlet、Listener的學(xué)習(xí)_動力節(jié)點Java學(xué)院整理
- Spring Boot 編寫Servlet、Filter、Listener、Interceptor的方法
- 解決cannot be cast to javax.servlet.Filter 報錯的問題
- Servlet Filter過濾器執(zhí)行順序
相關(guān)文章
springboot配置數(shù)據(jù)庫密碼特殊字符報錯的解決
這篇文章主要介紹了springboot配置數(shù)據(jù)庫密碼特殊字符報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
java對象list使用stream根據(jù)某一個屬性轉(zhuǎn)換成map的3種方式舉例
開發(fā)小伙伴們通常會需要使用到對象和Map互相轉(zhuǎn)換的開發(fā)場景,下面這篇文章主要給大家介紹了關(guān)于java對象list使用stream根據(jù)某一個屬性轉(zhuǎn)換成map的3種方式,需要的朋友可以參考下2024-01-01
解決mybatis返回boolean值時數(shù)據(jù)庫返回null的問題
這篇文章主要介紹了解決mybatis返回boolean值時數(shù)據(jù)庫返回null的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Java實現(xiàn)數(shù)據(jù)脫敏(Desensitization)的操作指南
數(shù)據(jù)脫敏是指通過對敏感數(shù)據(jù)進(jìn)行部分或完全隱藏處理,保護(hù)敏感信息在存儲和使用過程中的安全性,常見的應(yīng)用場景包括日志記錄、接口返回、報表展示、數(shù)據(jù)分析等,本文給大家介紹了Java實現(xiàn)數(shù)據(jù)脫敏(Desensitization)的操作指南,需要的朋友可以參考下2025-02-02
idea使用spring Initializr 快速搭建springboot項目遇到的坑
這篇文章主要介紹了idea使用spring Initializr 快速搭建springboot項目遇到的坑,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
Javaweb開發(fā)環(huán)境Myeclipse6.5 JDK1.6 Tomcat6.0 SVN1.8配置教程
這篇文章主要介紹了Javaweb開發(fā)環(huán)境Myeclipse6.5 JDK1.6 Tomcat6.0 SVN1.8配置教程,感興趣的小伙伴們可以參考一下2016-06-06

