網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
業(yè)務(wù)需求
通過(guò)Servlet模擬監(jiān)聽(tīng)器, 實(shí)現(xiàn)部署應(yīng)用程序加載時(shí)并初始化servlet,來(lái)獲取自定義的上傳路徑
解決方案
Servlet設(shè)啟動(dòng)級(jí)別loadOnStartup : 標(biāo)記容器是否在啟動(dòng)的時(shí)候就加載這個(gè)servlet。
<load-on-startup>1</load-on-startup>
當(dāng)值為0或者大于0時(shí),表示容器在應(yīng)用啟動(dòng)時(shí)就加載這個(gè)servlet;
當(dāng)是一個(gè)負(fù)數(shù)時(shí)或者沒(méi)有指定時(shí),則指示訪問(wèn)容器中servle的urlPatterns映射路徑時(shí)才加載。
loadOnStartup 配置加載 啟動(dòng)順序 值越小 越優(yōu)先 必須同時(shí)配置屬性 value 否則無(wú)法直接加載
而在測(cè)試需求中
此Servlet需要項(xiàng)目加載時(shí)就需要獲取的上傳路徑,而是不需要URL訪問(wèn) 所以不需要配置 servlet-mapping 訪問(wèn)映射 urlPatterns
注解實(shí)現(xiàn)
Servlet3.0提供了注解(annotation),使得不再需要在web.xml文件中進(jìn)行Servlet的部署描述
完成了一個(gè)使用注解描述的Servlet程序開(kāi)發(fā)。
使用@WebServlet將一個(gè)繼承于javax.servlet.http.HttpServlet的類定義為Servlet組件。
@WebServlet有很多的屬性:
1、asyncSupported: 聲明Servlet是否支持異步操作模式。
2、description: Servlet的描述。
3、displayName: Servlet的顯示名稱。
4、initParams: Servlet的init參數(shù)。
5、name: Servlet的名稱。
6、urlPatterns: Servlet的訪問(wèn)URL。
7、value: Servlet的訪問(wèn)URL。
Servlet的訪問(wèn)URL是Servlet的必選屬性,可以選擇使用urlPatterns或者value定義。
原理分析
Servlet specification:
The load-on-startup element indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the web application. The optional contents of these element must be an integer indicating the order in which the servlet should be loaded. If the value is a negative integer, or the element is not present, the container is free to load the servlet whenever it chooses. If the value is a positive integer or 0, the container must load and initialize the servlet as the application is deployed. The container must guarantee that servlets marked with lower integers are loaded before servlets marked with higher integers. The container may choose the order of loading of servlets with the same load-on-start-up value.
翻譯意思大概如下:
1)load-on-startup 元素標(biāo)記容器是否應(yīng)該web應(yīng)用程序啟動(dòng)時(shí)加載(實(shí)例化并調(diào)用init())
2)它的值必須是一個(gè)整數(shù),表示servlet應(yīng)該被載入的順序
3)如果該元素不存在或者這個(gè)數(shù)為負(fù)時(shí),則容器會(huì)當(dāng)該Servlet被請(qǐng)求時(shí),再加載。
4)當(dāng)值為0或者大于0時(shí),表示容器必須在部署應(yīng)用程序時(shí)加載并初始化servlet
5)正數(shù)的值越小,該servlet的優(yōu)先級(jí)越高,應(yīng)用啟動(dòng)時(shí)就越先加載。
6)當(dāng)值相同時(shí),容器就會(huì)自己選擇順序來(lái)加載。
代碼測(cè)試
/**
* 此Servlet不需要URL訪問(wèn) 所以不需要配置 servlet-mapping 訪問(wèn)映射 urlPatterns
* loadOnStartup 配置加載 啟動(dòng)順序 值越小 越優(yōu)先 必須同時(shí)配置屬性 value 否則無(wú)法激活
* company 源辰信息
* @author 38929
* @date 2021年7月28日
* @version 1.0
* Email 1198865589@qq.com
*/
@WebServlet( name="InitServlet",loadOnStartup = 1, value="",
initParams = { @WebInitParam(name="uplaodPath", value="../103_5_images_goods")})
public class InitServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String path = "../images";
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("Servlet初始化上傳路徑配置");
//獲取初始化參數(shù)
String temp = config.getInitParameter("uplaodPath");
System.out.println( temp );
}
}
效果展示
注意事項(xiàng)
在注解中必須同時(shí)配置屬性 value="" 否則Servlet將無(wú)法被加載 控制臺(tái)無(wú)輸出
參考博客:
原文鏈接:https://blog.csdn.net/Klhz555/article/details/119321434
相關(guān)推薦
- 2022-03-29 redis的list數(shù)據(jù)類型相關(guān)命令介紹及使用_Redis
- 2022-10-14 C++ STL - list 模擬實(shí)現(xiàn)+解析迭代器
- 2022-06-14 Github?Copilot結(jié)合python的使用方法詳解_python
- 2023-04-08 C語(yǔ)言字符串左旋的兩種實(shí)現(xiàn)方法_C 語(yǔ)言
- 2022-06-20 音視頻基本概念和FFmpeg的簡(jiǎn)單入門教程詳解_相關(guān)技巧
- 2022-03-06 C語(yǔ)言之快速排序算法(遞歸Hoare版)介紹_C 語(yǔ)言
- 2022-10-14 linux服務(wù)器安裝redis并設(shè)置redis開(kāi)機(jī)自啟和遠(yuǎn)程連接
- 2022-09-17 C++實(shí)現(xiàn)圖的遍歷算法(DFS,BFS)的示例代碼_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支