servlet擷取並儲存web.xml中context-param參數

來源:互聯網
上載者:User

標籤:

在web.xml中定義了context-param,一般不會隨意改動,所以在監聽器中做一次處理,容器啟動時讀取並儲存在Properties中,方便以後取值。

SysProperties 類用於儲存 context 索引值;

SystemListener 監聽器類,處理 context-param 參數。

/** * 用於儲存參數索引值 */public final class SysProperties {    private static SysProperties instance;    private Properties initProperties = new Properties();    private SysProperties() { }    public static SysProperties getInstance() {        if (instance == null) {            instance = new SysProperties();        }        return instance;    }    /**     * 擷取對應的值     * @param key String 提供的鍵(param-name)     * @return String 鍵對應的值(param-value)     */    public String getProperty(String key)    {        return this.initProperties.getProperty(key);    }    /**     * 檢測是否包含該鍵     * @param key String 鍵     * @return boolean 該鍵存在返回 true,否則返回 false     */    public boolean containsKey(String key) {        return this.initProperties.containsKey(key);    }    /**     * 儲存參數     * @param key String param-name     * @param object String param-value     */    public void put(String key, String object) {        this.initProperties.put(key, object);    }}

 

/** * 系統監聽,程式啟動時初始化並儲存相關參數 */public class SystemListener implements ServletContextListener {    @Override    public void contextInitialized(ServletContextEvent servletContextEvent) {        initContextParam(servletContextEvent);    }    @Override    public void contextDestroyed(ServletContextEvent servletContextEvent) { }    private void initContextParam(ServletContextEvent event) {        Enumeration<String> names = event.getServletContext().getInitParameterNames();        while (names.hasMoreElements())        {            String name = names.nextElement();            String value = event.getServletContext().getInitParameter(name);            SysProperties.getInstance().put(name, value);        }    }}

servlet擷取並儲存web.xml中context-param參數

聯繫我們

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