国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實現(xiàn)方法

2020-07-27 21:19:28
字體:
供稿:網(wǎng)友

四大作用域:

Web應(yīng)用中的變量存放在不同的jsp對象中,會有不一樣的作用域,四種不同的作用域排序是 pageContext < request < session < application;

1、pageContext:頁面域,僅當前頁面有效,離開頁面后,不論重定向還是轉(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當中

現(xiàn)在需要在所有的jsp中都能通過EL表達式讀取到properties中的屬性,并且是針對所有的會話,故這里利用application作用域,

那么什么時候?qū)roperties中的屬性存儲到application呢?因為是將properties的屬性值作為全局的變量以方便任何一次EL的獲取,所以在web應(yīng)用加載的時候就將值存儲到application當中,

這里就要利用ServletContextListener:

ServletContextListener是Servlet API 中的一個接口,它能夠監(jiān)聽 ServletContext 對象的生命周期,實際上就是監(jiān)聽 Web 應(yīng)用的生命周期。

當Servlet 容器啟動或終止Web 應(yīng)用時,會觸發(fā)ServletContextEvent 事件,該事件由ServletContextListener 來處理。

具體步驟如下:

1、新建一個類PropertyListenter實現(xiàn) ServletContextListener接口的contextInitialized方法;

2、讀取properties配置文件,轉(zhuǎn)存到Map當中;

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 {   //通過類加載器進行獲取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> 

配置好后,運行Web應(yīng)用,就能在所有的jsp頁面中用EL表達式獲取到properties中的屬性值了。

以上這篇將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 铜陵市| 泾川县| 崇文区| 民和| 中宁县| 浦江县| 拜城县| 霍州市| 商南县| 石泉县| 阆中市| 额尔古纳市| 阿勒泰市| 赫章县| 阜阳市| 黎平县| 永寿县| 鞍山市| 深泽县| 正镶白旗| 焦作市| 新余市| 富顺县| 贞丰县| 嘉祥县| 珠海市| 缙云县| 临汾市| 舞阳县| 如皋市| 尼木县| 福鼎市| 永靖县| 余江县| 柳州市| 祁连县| 邯郸县| 名山县| 松滋市| 柘荣县| 尚义县|