在 Servlet API 中有一個 ServletContextListener 接口,它可以監聽 ServletContext 對象的生命週期,實際上就是監聽 Web 應用的生命週期。html
當Servlet 容器啓動或終止Web 應用時,會觸發ServletContextEvent 事件,該事件由ServletContextListener 來處理。在 ServletContextListener 接口中定義了處理ServletContextEvent 事件的兩個方法。web
/** 數據庫
* 當Servlet 容器啓動Web 應用時調用該方法。在調用完該方法以後,容器再對Filter 初始化, 服務器
* 而且對那些在Web 應用啓動時就須要被初始化的Servlet 進行初始化。 app
*/ ide
contextInitialized(ServletContextEvent sce) 函數
/** post
* 當Servlet 容器終止Web 應用時調用該方法。在調用該方法以前,容器會先銷燬全部的Servlet 和Filter 過濾器。 this
*/
contextDestroyed(ServletContextEvent sce)
下面經過兩個具體的例子來介紹 ServletContextListener 的用法。
例一:在服務啓動時,將數據庫中的數據加載進內存,並將其賦值給一個屬性名,其它的 Servlet 就能夠經過 getAttribute 進行屬性值的訪問。
有以下兩個步驟:
1 : ServletContext 對象是一個爲整個 web 應用提供共享的內存,任何請求均可以訪問裏面的內容
2 :如何實如今服務啓動的時候就動態的加入到裏面的內容:咱們須要作的有:
1 ) 實現 servletContextListerner 接口 並將要共享的經過 setAttribute ( name,data )方法提交到內存中去 ;
2 )應用項目經過 getAttribute(name) 將數據取到 。
public class ServletContextLTest implements ServletContextListener{
// 實現其中的銷燬函數
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("this is last destroyeed");
}
// 實現其中的初始化函數,當有事件發生時即觸發
public void contextInitialized(ServletContextEvent sce) {
ServletContext sct=sce.getServletContext();
Map<Integer,String> depts=new HashMap<Integer,String>();
Connection connection=null;
PreparedStatement pstm=null;
ResultSet rs=null;
try{
connection=ConnectTool.getConnection();
String sql="select deptNo,dname from dept";
pstm=connection.prepareStatement(sql);
rs=pstm.executeQuery();
while(rs.next()){
depts.put(rs.getInt(1), rs.getString(2));
}
// 將所取到的值存放到一個屬性鍵值對中
sct.setAttribute("dept", depts);
System.out.println("======listener test is beginning=========");
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectTool.releasersc(rs, pstm, connection);
}
}
}
在完成上述編碼後,仍需在 web.xml 中進行以下配置,以使得該監聽器能夠起做用。
<listener>
<listener-class>ServletContextTest.ServletContextLTest</listener-class>
</listener>
在完成上述配置後, web 服務器在啓動時,會直接加載該監聽器,經過如下的應用程序就能夠進行數據的訪問。
public class CreateEmployee extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext sct=getServletConfig().getServletContext();
// 從上下文環境中經過屬性名獲取屬性值
Map<Integer,String> dept=(Map<Integer,String>)sct.getAttribute("dept");
Set<Integer> key=dept.keySet();
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<form action='/register' action='post'>");
out.println("<table alignb='center'>");
out.println("<tr>");
out.println("<td>");
out.println("username:");
out.println("</td>");
out.println("<td>");
out.println("<input type='text' name='username'");
out.println("</tr>");
out.println("<tr>");
out.println("<td>");
out.println("city:");
out.println("</td>");
out.println("<td>");
out.println("<select name='dept'");
for(Integer i:key){
out.println("<option value='"+i+"'>"+dept.get(i)+"</option>");
}
out.println("</select>");
out.println("</td>");
out.println("<tr>");
out.println("</table>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
out.flush();
}
}
例二:書寫一個類用於統計當Web 應用啓動後,網頁被客戶端訪問的次數。若是從新啓動Web 應用,計數器不會從新從1 開始統計訪問次數,而是從上次統計的結果上進行累加。
在實際應用中,每每須要統計自Web 應用被髮布後網頁被客戶端訪問的次數,這就要求當Web 應用被終止時,計數器的數值被永久存儲在一個文件中或者數據庫中,等到Web 應用從新啓動時,先從文件或數據庫中讀取計數器的初始值,而後在此基礎上繼續計數。
向文件中寫入或讀取計數器的數值的功能能夠由自定義的 MyServletContextListener 類來完成,它具備如下功能:
1 、在 Web 應用啓動時從文件中讀取計數器的數值,並把表示計數器的 Counter 對象存放到 Web應用範圍內。存放計數器的文件的路徑爲helloapp/count/count.txt 。
2 、在Web 應用終止時把Web 應用範圍內的計數器的數值保存到count.txt 文件中。
public class MyServletContextListener implements ServletContextListener{
public void contextInitialized(ServletContextEvent sce){
System.out.println("helloapp application is Initialized.");
// 獲取 ServletContext 對象
ServletContext context=sce.getServletContext();
try{
// 從文件中讀取計數器的數值
BufferedReader reader=new BufferedReader(
new InputStreamReader(context.
getResourceAsStream("/count/count.txt")));
int count=Integer.parseInt(reader.readLine());
reader.close();
// 建立計數器對象
Counter counter=new Counter(count);
// 把計數器對象保存到 Web 應用範圍
context.setAttribute("counter",counter);
} catch(IOException e) {
e.printStackTrace();
}
}
public void contextDestroyed(ServletContextEvent sce){
System.out.println("helloapp application is Destroyed.");
// 獲取 ServletContext 對象
ServletContext context=sce.getServletContext();
// 從 Web 應用範圍得到計數器對象
Counter counter=(Counter)context.getAttribute("counter");
if(counter!=null){
try{
// 把計數器的數值寫到 count.txt 文件中
String filepath=context.getRealPath("/count");
filepath=filepath+"/count.txt";
PrintWriter pw=new PrintWriter(filepath);
pw.println(counter.getCount());
pw.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
將用戶自定義的 MyServletContextListener 監聽器在 Servlet 容器進行註冊, Servlet 容器會在啓動或終止 Web 應用時,會調用該監聽器的相關方法。在 web.xml 文件中, <listener> 元素用於向容器註冊監聽器:
<listener>
<listener-class> ServletContextTest .MyServletContextListener<listener-class />
</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