日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Servlet配置啟動(dòng)級(jí)別loadOnStartup注意事項(xiàng)

作者:夢(mèng)凝哲雪 更新時(shí)間: 2022-02-25 編程語(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ú)輸出

參考博客:

Servlet的配置參數(shù)load-on-startup參數(shù)理解

原文鏈接:https://blog.csdn.net/Klhz555/article/details/119321434

欄目分類
最近更新