【轉】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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM