ServletContextListener使用詳解 .

在 Servlet API 中有一個 ServletContextListener 接口,它可以監聽 ServletContext 對象的生命週期,實際上就是監聽 Web 應用的生命週期。html

當Servlet 容器啓動或終止Web 應用時,會觸發ServletContextEvent 事件,該事件由ServletContextListener 來處理。在 ServletContextListener 接口中定義了處理ServletContextEvent 事件的兩個方法。web

Java代碼  收藏代碼sql

  1. /** 數據庫

  2.  * 當Servlet 容器啓動Web 應用時調用該方法。在調用完該方法以後,容器再對Filter 初始化, 服務器

  3.  * 而且對那些在Web 應用啓動時就須要被初始化的Servlet 進行初始化。 app

  4.  */  ide

  5. contextInitialized(ServletContextEvent sce)   函數

  6.   

  7.   

  8. /** post

  9.  * 當Servlet 容器終止Web 應用時調用該方法。在調用該方法以前,容器會先銷燬全部的Servlet 和Filter 過濾器。 this

  10.  */  

  11. contextDestroyed(ServletContextEvent sce)  

 

 下面經過兩個具體的例子來介紹 ServletContextListener 的用法。

 

例一:在服務啓動時,將數據庫中的數據加載進內存,並將其賦值給一個屬性名,其它的 Servlet 就能夠經過 getAttribute 進行屬性值的訪問。

 

有以下兩個步驟:

 

1 : ServletContext 對象是一個爲整個 web 應用提供共享的內存,任何請求均可以訪問裏面的內容  

 

2 :如何實如今服務啓動的時候就動態的加入到裏面的內容:咱們須要作的有:  

1 ) 實現 servletContextListerner 接口 並將要共享的經過 setAttribute ( name,data )方法提交到內存中去   ;

2 )應用項目經過 getAttribute(name) 將數據取到 。

 

Java代碼  收藏代碼

  1. public class ServletContextLTest implements ServletContextListener{   

  2.   

  3.     // 實現其中的銷燬函數  

  4.       

  5.     public void contextDestroyed(ServletContextEvent sce) {   

  6.   

  7.         System.out.println("this is last destroyeed");      

  8.   

  9.     }   

  10.   

  11.     // 實現其中的初始化函數,當有事件發生時即觸發  

  12.   

  13.     public void contextInitialized(ServletContextEvent sce) {   

  14.   

  15.         ServletContext sct=sce.getServletContext();   

  16.   

  17.         Map<Integer,String> depts=new HashMap<Integer,String>();   

  18.   

  19.         Connection connection=null;   

  20.   

  21.         PreparedStatement pstm=null;   

  22.   

  23.         ResultSet rs=null;   

  24.   

  25.            

  26.   

  27.         try{   

  28.   

  29.             connection=ConnectTool.getConnection();   

  30.   

  31.             String sql="select deptNo,dname from dept";   

  32.   

  33.             pstm=connection.prepareStatement(sql);   

  34.   

  35.             rs=pstm.executeQuery();   

  36.   

  37.             while(rs.next()){   

  38.   

  39.                 depts.put(rs.getInt(1), rs.getString(2));   

  40.   

  41.             }   

  42.   

  43.             // 將所取到的值存放到一個屬性鍵值對中  

  44.   

  45.             sct.setAttribute("dept", depts);   

  46.   

  47.             System.out.println("======listener test is beginning=========");   

  48.   

  49.         }catch(Exception e){   

  50.   

  51.             e.printStackTrace();   

  52.   

  53.         }finally{   

  54.   

  55.             ConnectTool.releasersc(rs, pstm, connection);   

  56.   

  57.         }   

  58.   

  59.     }   

  60.   

  61. }  

 

在完成上述編碼後,仍需在 web.xml 中進行以下配置,以使得該監聽器能夠起做用。

 

Xml代碼  收藏代碼

  1. <listener>   

  2.   

  3.    <listener-class>ServletContextTest.ServletContextLTest</listener-class>   

  4.   

  5. </listener>    

 

在完成上述配置後, web 服務器在啓動時,會直接加載該監聽器,經過如下的應用程序就能夠進行數據的訪問。

 

Java代碼  收藏代碼

  1. public class CreateEmployee extends HttpServlet{   

  2.   

  3.     @Override   

  4.   

  5.     protected void service(HttpServletRequest request, HttpServletResponse response)   

  6.   

  7.             throws ServletException, IOException {   

  8.   

  9.         ServletContext sct=getServletConfig().getServletContext();   

  10.   

  11.         // 從上下文環境中經過屬性名獲取屬性值  

  12.   

  13.         Map<Integer,String> dept=(Map<Integer,String>)sct.getAttribute("dept");   

  14.   

  15.         Set<Integer> key=dept.keySet();   

  16.   

  17.         response.setContentType("text/html;charset=utf-8");   

  18.   

  19.         PrintWriter out=response.getWriter();   

  20.   

  21.         out.println("<html>");   

  22.   

  23.         out.println("<body>");   

  24.   

  25.         out.println("<form action='/register' action='post'>");   

  26.   

  27.         out.println("<table alignb='center'>");   

  28.   

  29.         out.println("<tr>");   

  30.   

  31.         out.println("<td>");   

  32.   

  33.         out.println("username:");   

  34.   

  35.         out.println("</td>");   

  36.   

  37.         out.println("<td>");   

  38.   

  39.         out.println("<input type='text' name='username'");   

  40.   

  41.         out.println("</tr>");   

  42.   

  43.         out.println("<tr>");   

  44.   

  45.         out.println("<td>");   

  46.   

  47.         out.println("city:");   

  48.   

  49.         out.println("</td>");   

  50.   

  51.         out.println("<td>");   

  52.   

  53.         out.println("<select name='dept'");   

  54.   

  55.         for(Integer i:key){   

  56.   

  57.             out.println("<option value='"+i+"'>"+dept.get(i)+"</option>");   

  58.   

  59.         }   

  60.   

  61.         out.println("</select>");   

  62.   

  63.         out.println("</td>");   

  64.   

  65.         out.println("<tr>");   

  66.   

  67.         out.println("</table>");   

  68.   

  69.         out.println("</form>");   

  70.   

  71.         out.println("</body>");   

  72.   

  73.         out.println("</html>");   

  74.   

  75.         out.flush();   

  76.   

  77.     }   

  78.   

  79. }  

 

例二:書寫一個類用於統計當Web 應用啓動後,網頁被客戶端訪問的次數。若是從新啓動Web 應用,計數器不會從新從1 開始統計訪問次數,而是從上次統計的結果上進行累加。

 

在實際應用中,每每須要統計自Web 應用被髮布後網頁被客戶端訪問的次數,這就要求當Web 應用被終止時,計數器的數值被永久存儲在一個文件中或者數據庫中,等到Web 應用從新啓動時,先從文件或數據庫中讀取計數器的初始值,而後在此基礎上繼續計數。

 

向文件中寫入或讀取計數器的數值的功能能夠由自定義的 MyServletContextListener 類來完成,它具備如下功能:

 

1 、在 Web 應用啓動時從文件中讀取計數器的數值,並把表示計數器的 Counter 對象存放到 Web應用範圍內。存放計數器的文件的路徑爲helloapp/count/count.txt 。

2 、在Web 應用終止時把Web 應用範圍內的計數器的數值保存到count.txt 文件中。

Java代碼  收藏代碼

  1. public class MyServletContextListener implements ServletContextListener{  

  2.   

  3.   public void contextInitialized(ServletContextEvent sce){  

  4.   

  5.     System.out.println("helloapp application is Initialized.");  

  6.   

  7.     // 獲取 ServletContext 對象  

  8.   

  9.     ServletContext context=sce.getServletContext();  

  10.   

  11.     try{  

  12.   

  13.        // 從文件中讀取計數器的數值  

  14.   

  15.        BufferedReader reader=new BufferedReader(  

  16.   

  17.            new InputStreamReader(context.  

  18.   

  19.            getResourceAsStream("/count/count.txt")));  

  20.   

  21.        int count=Integer.parseInt(reader.readLine());  

  22.   

  23.        reader.close();  

  24.   

  25.        // 建立計數器對象  

  26.   

  27.        Counter counter=new Counter(count);  

  28.   

  29.        // 把計數器對象保存到 Web 應用範圍  

  30.   

  31.        context.setAttribute("counter",counter);  

  32.   

  33.        } catch(IOException e) {  

  34.   

  35.           e.printStackTrace();  

  36.   

  37.        }  

  38.   

  39.    }  

  40.   

  41.    public void contextDestroyed(ServletContextEvent sce){  

  42.   

  43.        System.out.println("helloapp application is Destroyed.");  

  44.   

  45.        // 獲取 ServletContext 對象  

  46.   

  47.        ServletContext context=sce.getServletContext();  

  48.   

  49.        // 從 Web 應用範圍得到計數器對象  

  50.   

  51.        Counter counter=(Counter)context.getAttribute("counter");  

  52.   

  53.        if(counter!=null){  

  54.   

  55.        try{  

  56.   

  57.           // 把計數器的數值寫到 count.txt 文件中  

  58.   

  59.           String filepath=context.getRealPath("/count");  

  60.   

  61.           filepath=filepath+"/count.txt";  

  62.   

  63.           PrintWriter pw=new PrintWriter(filepath);  

  64.   

  65.           pw.println(counter.getCount());  

  66.   

  67.           pw.close();  

  68.   

  69.          } catch(IOException e) {  

  70.   

  71.              e.printStackTrace();  

  72.   

  73.          }  

  74.   

  75.      }  

  76.   

  77.    }  

  78.   

  79. }   

 

將用戶自定義的 MyServletContextListener 監聽器在 Servlet 容器進行註冊, Servlet 容器會在啓動或終止 Web 應用時,會調用該監聽器的相關方法。在 web.xml 文件中, <listener> 元素用於向容器註冊監聽器:

Xml代碼  收藏代碼

  1. <listener>  

  2. <listener-class> ServletContextTest .MyServletContextListener<listener-class />  

  3. </listener>   

 

經過上述兩個例子,便可以很是清楚的瞭解到 ServletContextListener 接口的使用方法及技巧。

 

在Container 加載Web 應用程序時(例如啓動 Container 以後),會呼叫contextInitialized() ,而當容器移除Web 應用程序時,會呼叫contextDestroyed () 方法。

 

經過 Tomcat 控制檯的打印結果的前後順序,會發現當 Web 應用啓動時,Servlet 容器先調用contextInitialized() 方法,再調用lifeInit 的init() 方法;

當Web 應用終止時,Servlet 容器先調用lifeInit 的destroy() 方法,再調用contextDestroyed() 方法。

 

因而可知,在Web 應用的生命週期中,ServletContext 對象最先被建立,最晚被銷燬。

 

原文地址:http://blog.csdn.net/zhaozheng7758/archive/2010/12/28/6103700.aspx


摘自:http://blog.csdn.net/ethanq/article/details/7358346

相關文章
相關標籤/搜索