ServletConfig對象 【通過此對象擷取到web.xml中的資訊】

來源:互聯網
上載者:User

標籤:style   io   使用   ar   檔案   art   sp   on   c   

用途:
       1)想讓當前的Servlet讀取一些在web.xml檔案配置的初始化參數時,
                      可以使用ServletConfig對象,他是Servlet運行時的設定物件。
       2)init(ServletConfig config)方法由web容器調用,調用時會傳入與容器相關的ServletConfig介面的實現
                      所以此對象需要init(ServletConfig config)的配合使用

   
        一】web.xml部分
            需要加入標籤
                <init-param>
                    <param-name>參數名字</param-name>
                    <param-value>參數的值</param-value>
                </init-param>
            eg:
                <servlet>
                <servlet-name>Demo5</servlet-name>
                <servlet-class>cn.itcast.web.servlet.Demo5</servlet-class>
                <init-param>
                    <param-name>num</param-name>
                    <param-value>12101020504</param-value>
                </init-param>
                <init-param>
                    <param-name>tel</param-name>
                    <param-value>18381330428</param-value>
                </init-param>
                <init-param>
                    <param-name>charset</param-name>
                    <param-value>utf-8</param-value>
                </init-param>
                <load-on-startup>5</load-on-startup>/作用:部署時就載入,不用等到用戶端訪問請求時才載入。
              </servlet>
                
        二】ServletConfig介面:
            1)重點方法:
                1》根據web.xml檔案中的<Servlet>標籤下的<init-param>標籤中的標籤名獲得標籤值
                    String getInitParameter(String name);
                2》獲得web.xml檔案中<servlet>標籤下的<init-param>標籤下的所有標籤的標籤名,以此遍曆標籤
                    Enumeration getInitParameterNames();
                3》
                eg:
    
                    方法一: String getInitParameter(String name);方法
                    
                      String number = config.getInitParameter("num");
                      String tel =  config.getInitParameter("tel");
                      
                      System.out.println("number = " + number);//number = 12101020504
                      System.out.println("tel = " + tel);//tel = 18381330428
                    
                    
                    方法二: Enumeration getInitParameterNames()方法
                    
                    Enumeration en = config.getInitParameterNames();
                    while(en.hasMoreElements()){
                        String str = (String) en.nextElement();
                        System.out.println(str + "= " + config.getInitParameter(str));
                    }
                   


       

ServletConfig對象 【通過此對象擷取到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.