傳值:web.xml傳遞參數 即在Servlet中擷取web.xml裡的值

來源:互聯網
上載者:User

標籤:配置   generic   generics   傳遞參數   code   parameter   XML   context   覆蓋   

 1 傳值:web.xml傳遞參數 2  3 在web.xml中的Servlet裡配置多個init-param 4 <servlet> 5   ... 6   <init-param> 7     <param-name>name</param-name> 8     <param-value>關羽</param-value> 9   </init-param>10   <init-param>11     <param-name>age</param-name>12     <param-value>20</param-value>13   </init-param>14   <init-param>15     <param-name>sex</param-name>16     <param-value>男</param-value>17   </init-param>18 </servlet>19 20 在servlet的init()方法中:21 String name=this.getServletConfig().getInitParameter("name");22 String age=this.getServletConfig().getInitParameter("age");23 24 25 ServletConfig sc=this.getServletConfig();
//枚舉 擷取所有配置參數資訊
26 Enumeration e=sc.getServletConfig().getInitParameterNames();27 while(e.hasMoreElements()){28 syso(sc.getInitParameter(e.nextElement().toString()));29 }30 31 32 -----------------------------------33 在web.xml中的web-app下配置多個context-param34 <context-param>35 <param-name></param-name>36 <param-value></param-value>37 </context-param>38 39 <context-param>40 <param-name></param-name>41 <param-value></param-value>42 </context-param>43 44 45 context:一般指上下文環境46 47 ServletContext:指的是整個web項目的上下文環境 ,表示的就是整個web.xml48 49 this.getServletContext().getInitParameter("age");50 //可以通過局部獲得整體(由子到父)51 this.getServletConfig().getServletContext().getInitParameter("age");52 53 ServletConfig,ServletContext 可以直接從this(Servlet)中獲得這兩個對象
54 ServletConfig scf=this.getServletConfig();55 ServletContext sct=this.getServletContext();56 57 init(),init(ServletConfig config):在GenericServlet中,init(ServletConfig config) 裡面調用init(),所以覆蓋的是init(),而不是init(ServletConfig config)

 

傳值:web.xml傳遞參數 即在Servlet中擷取web.xml裡的值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.