將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實現(xiàn)方法
四大作用域:
Web應(yīng)用中的變量存放在不同的jsp對象中,會有不一樣的作用域,四種不同的作用域排序是 pageContext < request < session < application;
1、pageContext:頁面域,僅當(dāng)前頁面有效,離開頁面后,不論重定向還是轉(zhuǎn)向(即無論是redirect還是forward),pageContext的屬性值都失效;
2、request:請求域,在一次請求中有效,如果用forward轉(zhuǎn)向,則下一次請求還可以保留上一次request中的屬性值,而redirect重定向跳轉(zhuǎn)到另一個頁面則會使上一次request中的屬性值失效;
3、session:會話域,在一次會話過程中(從瀏覽器打開到瀏覽器關(guān)閉這個過程),session對象的屬性值都保持有效,在這次會話過程,session中的值可以在任何頁面獲??;
4、application:應(yīng)用域,只要應(yīng)用不關(guān)閉,該對象中的屬性值一直有效,并且為所有會話所共享。
利用ServletContextListener監(jiān)聽器,一旦應(yīng)用加載,就將properties的值存儲到application當(dāng)中
現(xiàn)在需要在所有的jsp中都能通過EL表達(dá)式讀取到properties中的屬性,并且是針對所有的會話,故這里利用application作用域,
那么什么時候?qū)roperties中的屬性存儲到application呢?因為是將properties的屬性值作為全局的變量以方便任何一次EL的獲取,所以在web應(yīng)用加載的時候就將值存儲到application當(dāng)中,
這里就要利用ServletContextListener:
ServletContextListener是Servlet API 中的一個接口,它能夠監(jiān)聽 ServletContext 對象的生命周期,實際上就是監(jiān)聽 Web 應(yīng)用的生命周期。
當(dāng)Servlet 容器啟動或終止Web 應(yīng)用時,會觸發(fā)ServletContextEvent 事件,該事件由ServletContextListener 來處理。
具體步驟如下:
1、新建一個類PropertyListenter實現(xiàn) ServletContextListener接口的contextInitialized方法;
2、讀取properties配置文件,轉(zhuǎn)存到Map當(dāng)中;
3、使用ServletContext對象將Map存儲到application作用域中;
/** * 設(shè)值全局變量 * @author meikai * @version 2017年10月23日 下午2:15:19 */ public class PropertyListenter implements ServletContextListener { /* (non-Javadoc) * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent) */ @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } /* (non-Javadoc) * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent) */ @Override public void contextInitialized(ServletContextEvent sce) { /** * 讀取properties文件 * */ final Logger logger = (Logger) LoggerFactory.getLogger(PropertyListenter.class); Properties properties = new Properties(); InputStream in = null; try { //通過類加載器進(jìn)行獲取properties文件流 in = PropertiesUtil.class.getClassLoader().getResourceAsStream("kenhome-common.properties"); properties.load(in); } catch (FileNotFoundException e) { logger.error("未找到properties文件"); } catch (IOException e) { logger.error("發(fā)生IOException異常"); } finally { try { if(null != in) { in.close(); } } catch (IOException e) { logger.error("properties文件流關(guān)閉出現(xiàn)異常"); } } /** * 將properties文件轉(zhuǎn)存到map */ Map<String, String> pros = new HashMap<String,String>((Map)properties); /** * 將Map通過ServletContext存儲到全局作用域中 */ ServletContext sct=sce.getServletContext(); sct.setAttribute("pros", pros); } }
4、在web.xml中配置上面的的監(jiān)聽器PropertyListenter:
<!-- 全局變量監(jiān)聽器,讀取properties文件,設(shè)值為全局變量 --> <listener> <listener-class>com.meikai.listener.PropertyListenter</listener-class> </listener>
配置好后,運(yùn)行Web應(yīng)用,就能在所有的jsp頁面中用EL表達(dá)式獲取到properties中的屬性值了。
以上這篇將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JSP Session超時設(shè)置的實現(xiàn)方法
這篇文章主要介紹了JSP Session超時設(shè)置的實現(xiàn)方法的相關(guān)資料,這里提供三種方法幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-09-09搭建Eclipse+MyEclipse開發(fā)環(huán)境
搭建Eclipse+MyEclipse開發(fā)環(huán)境...2007-02-02Jsp+Servlet實現(xiàn)文件上傳下載 文件上傳(一)
這篇文章主要為大家詳細(xì)介紹了Jsp+Servlet實現(xiàn)文件上傳下載中的第一部分文件上傳,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01JSP中獲取ExtJS.Ajax前臺傳遞的JSON數(shù)據(jù)實現(xiàn)過程
JSON數(shù)據(jù)接收的特定過程必須的數(shù)據(jù)包:commons-lang,commons-beanutils等等,否則JSONObject 報錯,并且不能接收,感興趣的朋友可以參考下哈2013-04-04