java的web.xml中context-param與init-param的區別介紹

來源:互聯網
上載者:User

web.xml中<context-param>與<init-param>的區別在於<context-param>設定的是一個在應用中全域(ServletContext範圍內)可見的參數,而<init-param>設定的是一個在應用中局部(ServletRequest範圍內)可見的參數。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>testdemo</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
  <context-param>
   <param-name>appversion</param-name>
   <param-value>1.0</param-value>
  </context-param>
 
  <servlet>
   <servlet-name>test</servlet-name>
   <servlet-class>test.TestServlet</servlet-class>
   <init-param>
    <param-name>name</param-name>
    <param-value>yedward</param-value>
   </init-param>
  </servlet>
 
  <servlet-mapping>
   <servlet-name>test</servlet-name>
   <url-pattern>/test</url-pattern>
  </servlet-mapping>
</web-app>
package test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {
  private static final long serialVersionUID = 5222793251610509039L;

  @Override
  public void init() throws ServletException {
    System.out.println(this.getInitParameter("name"));
  }

  @Override
  protected void service(HttpServletRequest request, HttpServletResponse resposne)
      throws ServletException, IOException {
    System.out.println(this.getInitParameter("name"));
    System.out.println(this.getServletContext().getInitParameter("appversion"));
    System.out.println(request.getServletContext().getInitParameter("appversion"));
  }
}

上面代碼的輸出結果是:

yedward
yedward
1.0
1.0
啟動一個WEB項目的時候,容器(如:Tomcat)會去設定檔web.xml讀兩個節點:<listener></listener>和<context-param></context-param>,容器建立一個ServletContext,這個WEB項目所有部分都將共用這個上下文,容器將<context-param></context-param>轉化為索引值對,並交給ServletContext;容器建立<listener></listener>中的類執行個體,即建立監聽,在監聽中會有contextInitialized(ServletContextEvent args)初始化方法,在這個方法中獲得:

ServletContext = ServletContextEvent.getServletContext();
context-param的值 = ServletContext.getInitParameter("context-param的鍵");
得到這個context-param的值之後,你就可以做一些操作了。注意,這個時候你的WEB項目還沒有完全啟動完成,這個動作會比所有的Servlet都要早。換句話說,這個時候,對<context-param>中的索引值做的操作,將在WEB項目完全啟動之前被執行。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.