1. 程式人生 > >ServletContext介紹及用法

ServletContext介紹及用法

1.1.  介紹
ServletContext官方叫servlet上下文。伺服器會為每一個工程建立一個物件,這個物件就是ServletContext物件。這個物件全域性唯一,而且工程內部的所有servlet都共享這個物件。所以叫全域性應用程式共享物件。

1.2.  作用
1.      是一個域物件

2.      可以讀取全域性配置引數

3.      可以搜尋當前工程目錄下面的資原始檔

4.      可以獲取當前工程名字(瞭解)

1.2.1.   servletContext是一個域物件
1.2.1.1.           域物件介紹
         域物件是伺服器在記憶體上建立的儲存空間,用於在不同動態資源(servlet)之間傳遞與共享資料。

1.2.1.2.           域物件方法
凡是域物件都有如下3個方法:

setAttribute(name,value);name是String型別,value是Object型別;

往域物件裡面新增資料,新增時以key-value形式新增

getAttribute(name);

根據指定的key讀取域物件裡面的資料

removeAttribute(name);

根據指定的key從域物件裡面刪除資料

1.2.1.3.           域物件功能程式碼
域物件儲存資料AddDataServlet程式碼

/**

      * doGet

      */

     publicvoid doGet(HttpServletRequest request, HttpServletResponse response)

              throws ServletException, IOException {

         //往serlvetContext裡面存資料

        

         //1.獲取ServletContext物件

         //getServletContext()

         //2.往物件裡面設定資料

         getServletContext().setAttribute("username", "admin");

        

         response.getOutputStream().write("使用者名稱寫入到servletContext成功".getBytes());

     }

 

獲取域物件資料GetDataServlet程式碼

/**

      * doGet

      */

     publicvoid doGet(HttpServletRequest request, HttpServletResponse response)

              throws ServletException, IOException {

        

         //獲取ServletContext裡面的使用者名稱資料

         Object valueObject = getServletContext().getAttribute("username");

         if(valueObject!=null){

              response.getOutputStream().write(("從servletContext讀取到的使用者名稱資料:"+valueObject.toString()).getBytes());

         }

 

     }

servletContext儲存資料特點,

全域性共享,裡面的資料所有動態資源都可以寫入和獲取

         伺服器啟動的時候建立,伺服器關閉的時候銷燬,因為這是全域性應用程式物件,全域性共享物件。

1.2.2.   可以讀取全域性配置引數
1.2.2.1.           servletContext讀取全域性引數核心方法
         getServletContext().getInitParameter(name);//根據指定的引數名獲取引數值

    getServletContext().getInitParameterNames();//獲取所有引數名稱列表

1.2.2.2.           實現步驟:
1.      在web.xml中配置全域性引數

  <!-- 全域性配置引數,因為不屬於任何一個servlet,但是所有的servlet都可以通過servletContext讀取這個資料 -->

  <context-param>

         <param-name>param1</param-name>

         <param-value>value1</param-value>

  </context-param>

   <context-param>

         <param-name>param2</param-name>

         <param-value>value2</param-value>

  </context-param>

 

 

2.      在動態資源servlet裡面使用servletcontext讀取全域性引數程式碼

public void doGet(HttpServletRequest request, HttpServletResponse response)

                    throws ServletException, IOException {

          

           //使用servletContext讀取全域性配置引數資料

           //核心方法

           /*getServletContext().getInitParameter(name);//根據指定的引數名獲取引數值

           getServletContext().getInitParameterNames();//獲取所有引數名稱列表*/

          

           //列印所有引數

           //1.先獲取所有全域性配置引數名稱

           Enumeration<String> enumeration =  getServletContext().getInitParameterNames();

           //2.遍歷迭代器

           while(enumeration.hasMoreElements()){

                    //獲取每個元素的引數名字

                    String parameName = enumeration.nextElement();

                    //根據引數名字獲取引數值

                    String parameValue = getServletContext().getInitParameter(parameName);

                    //列印

                    System.out.println(parameName+"="+parameValue);

           }

         }

1.2.3.   可以搜尋當前工程目錄下面的資原始檔
1.2.3.1.           核心方法
         getServletContext().getRealPath(path),根據相對路徑獲取伺服器上資源的絕對路徑

         getServletContext().getResourceAsStream(path),根據相對路徑獲取伺服器上資源的輸入位元組流

1.2.4.   可以獲取當前工程名字
1.2.4.1.           核心方法
         getServletContext().getContextPath();

         作用:獲取當前工程名字

1.2.4.2.           程式碼
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

       

        //獲取工程名字,getServletContext().getContextPath()

        response.getOutputStream().write(("工程名字:"+getServletContext().getContextPath()).getBytes());

 

    }


原文:https://blog.csdn.net/qq_36371449/article/details/80314024