溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JavaWeb中web.xml初始化加載順序詳解

發(fā)布時間:2020-10-10 21:32:37 來源:腳本之家 閱讀:227 作者: 欄目:編程語言

需求說明

做項目時,為了省事,起初把初始化的配置都放在每個類中 static加載,初始化配置一多,就想把它給整理一下,這里使用servlet中的init方法初始化。

web.xml說明

首先了解下web.xml中元素的加載順序:

  1. 啟動web項目后,web容器首先回去找web.xml文件,讀取這個文件
  2. 容器會創(chuàng)建一個 ServletContext ( servlet 上下文),整個 web 項目的所有部分都將共享這個上下文
  3. 容器將 轉(zhuǎn)換為鍵值對,并交給 servletContext
  4. 容器創(chuàng)建 中的類實(shí)例,創(chuàng)建監(jiān)聽器
  5. 容器加載filter,創(chuàng)建過濾器, 要注意對應(yīng)的filter-mapping一定要放在filter的后面
  6. 容器加載servlet,加載順序按照 Load-on-startup 來執(zhí)行

完整加載順序:ServletContext -> context-param -> listener-> filter -> servlet

配置實(shí)現(xiàn)

InitServlet.java:

/**
 * 初始化系統(tǒng)參數(shù)
 * 創(chuàng)建者 科幫網(wǎng)
 * 創(chuàng)建時間  2017年5月10日
 *
 */
public class InitServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  @Override
  public void init(){
    try {
      if(Constants.PAY_URL.size()==0){
        List<CommonEntity> listPayUrl = PropertiesListUtil.listPayUrl();
        for(CommonEntity entity:listPayUrl){
          Constants.PAY_URL.put(entity.getEntityCode(), entity.getEntityName());
        }
      }
      LogUtil.info("佛祖保佑    永不宕機(jī)   永無BUG :初始化系統(tǒng)數(shù)據(jù)數(shù)量:"+Constants.PAY_URL.size());
      Configs.init("zfbinfo.properties");
      LogUtil.info("初始化支付寶配置信息");
      SDKConfig.getConfig().loadPropertiesFromSrc();
      LogUtil.info("初始化銀聯(lián)支付配置信息");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * 重新加載配置文件
   * @Author  科幫網(wǎng)
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException 
   * @Date  2017年5月10日
   * 更新日志
   * 2017年5月10日 張志朋 首次創(chuàng)建
   *
   */
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    Constants.PAY_URL = new ConcurrentHashMap<String, String>();
    List<CommonEntity> listPayUrl = PropertiesListUtil.listPayUrl();
    for(CommonEntity entity:listPayUrl){
      Constants.PAY_URL.put(entity.getEntityCode(), entity.getEntityName());
    }
    LogUtil.info("初始化系統(tǒng)數(shù)據(jù)數(shù)量:"+Constants.PAY_URL.size());
  }
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }
}

web.xml:(部分配置)

<!-- 初始基礎(chǔ)化數(shù)據(jù)-->
  <servlet>
    <servlet-name>InitServlet</servlet-name>
    <servlet-class>com.acts.web.common.servlet.InitServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>InitServlet</servlet-name>
    <url-pattern>/InitServlet</url-pattern>
  </servlet-mapping>

servlet介紹

什么是servlet

servlet是sun公司為開發(fā)動態(tài)web而提供的一門技術(shù),用戶若想用發(fā)一個動態(tài)web資源(即開發(fā)一個Java程序向?yàn)g覽器輸出數(shù)據(jù)),需要完成以下2個步驟:

  1. 編寫一個Java類,實(shí)現(xiàn)servlet接口。
  2. 把開發(fā)好的Java類部署到web服務(wù)器中。

按照一種約定俗成的稱呼習(xí)慣,通常我們也把實(shí)現(xiàn)了servlet接口的java程序,稱之為Servlet。

servlet的運(yùn)行過程

  1. 瀏覽器發(fā)出請求,被web容器獲取到
  2. Web服務(wù)器首先檢查是否已經(jīng)裝載并創(chuàng)建了該Servlet的實(shí)例對象。如果是,則直接執(zhí)行第④步,否則,執(zhí)行第②步。
  3. 裝載并創(chuàng)建該Servlet的一個實(shí)例對象,調(diào)用Servlet實(shí)例對象的init()方法。
  4. 創(chuàng)建一個用于封裝HTTP請求消息的HttpServletRequest對象和一個代表HTTP響應(yīng)消息的HttpServletResponse對象,然后調(diào)用Servlet的service()方法并將請求和響應(yīng)對象作為參數(shù)傳遞進(jìn)去。
  5. WEB應(yīng)用程序被停止或重新啟動之前,Servlet引擎將卸載Servlet,并在卸載之前調(diào)用Servlet的destroy()方法

servlet初始化

  1. load-on-startup >=0 時,表示在web應(yīng)用啟動后立即加載,其中l(wèi)oad-on-startup的值越小,表示加載的優(yōu)先級越高,如果兩個servlet的load-on-startup值相同,則其加載優(yōu)先級有容器決定;
  2. load-on-startup 未配置時,則該servlet的加載由容器決定;

配置load-on-startup后,servlet在startup后立即加載,但只是調(diào)用servlet的init()方法,用以初始化該servlet相關(guān)的資源。初始化成功后,該servlet可響應(yīng)web請求;如未配置load-on-startup,容器一般在第一次響應(yīng)web請求時,會先檢測該servlet是否初始化,如未初始化,則調(diào)用servlet的init()先初始化,初始化成功后,再響應(yīng)請求。

PS:一般我們在開發(fā)web應(yīng)用時,都會配置這個參數(shù),有兩個好處:

  1. 如果初始化過程失敗,則容器會提示啟動失敗,此時我們能夠提前知道相關(guān)錯誤;
  2. 配置該參數(shù)相當(dāng)于將初始化servlet的工作轉(zhuǎn)移到容器啟動過程,使得容器只要啟動成功后,就可立即響應(yīng)web請求。

關(guān)于load-on-startup一些官網(wǎng)說明:

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.

注意

使用servlet時,一般都是繼承httpServlet,然后分別實(shí)現(xiàn)doGet或者doPost方法,但是在這里面要注意的是,這servlet并不是線程安全的,多線程單實(shí)例執(zhí)行的,當(dāng)并發(fā)訪問同一個資源的話(成員變量等等),就有可能引發(fā)線程安全問題。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI