精品熟女碰碰人人a久久,多姿,欧美欧美a v日韩中文字幕,日本福利片秋霞国产午夜,欧美成人禁片在线观看

將properties文件的配置設置為整個Web應用的全局變量實現方法

四大作用域:

web應用中的變量存放在不同的jsp對象中,會有不一樣的作用域,四種不同的作用域排序是 pagecontext < request < session < application;

1、pagecontext:頁面域,僅當前頁面有效,離開頁面后,不論重定向還是轉向(即無論是redirect還是forward),pagecontext的屬性值都失效;

2、request:請求域,在一次請求中有效,如果用forward轉向,則下一次請求還可以保留上一次request中的屬性值,而redirect重定向跳轉到另一個頁面則會使上一次request中的屬性值失效;

3、session:會話域,在一次會話過程中(從瀏覽器打開到瀏覽器關閉這個過程),session對象的屬性值都保持有效,在這次會話過程,session中的值可以在任何頁面獲??;

4、application:應用域,只要應用不關閉,該對象中的屬性值一直有效,并且為所有會話所共享。

利用servletcontextlistener監聽器,一旦應用加載,就將properties的值存儲到application當中

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

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

這里就要利用servletcontextlistener:

servletcontextlistener是servlet api 中的一個接口,它能夠監聽 servletcontext 對象的生命周期,實際上就是監聽 web 應用的生命周期。

當servlet 容器啟動或終止web 應用時,會觸發servletcontextevent 事件,該事件由servletcontextlistener 來處理。

具體步驟如下:

1、新建一個類propertylistenter實現 servletcontextlistener接口的contextinitialized方法;

2、讀取properties配置文件,轉存到map當中;

3、使用servletcontext對象將map存儲到application作用域中;

/**
 * 設值全局變量
 * @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("發生ioexception異常");
  } finally {
   try {
    if(null != in) {
     in.close();
    }
   } catch (ioexception e) {
    logger.error("properties文件流關閉出現異常");
   }
  }
      
  
  /**
   * 將properties文件轉存到map
   */
  map pros = new hashmap((map)properties);
  
  /**
   * 將map通過servletcontext存儲到全局作用域中
   */
  servletcontext sct=sce.getservletcontext(); 
  
  sct.setattribute("pros", pros);

 }
 

}

4、在web.xml中配置上面的的監聽器propertylistenter:

    com.meikai.listener.propertylistenter   

配置好后,運行web應用,就能在所有的jsp頁面中用el表達式獲取到properties中的屬性值了。

相關文章