摘自:http://blog.csdn.net/zhaozheng7758/article/details/6103700
在 Servlet API 中有一個(gè) ServletContextListener 接口,它能夠監(jiān)聽(tīng) ServletContext 對(duì)象的生命周期,實(shí)際上就是監(jiān)聽(tīng) Web 應(yīng)用的生命周期。
當(dāng)Servlet 容器啟動(dòng)或終止Web 應(yīng)用時(shí),會(huì)觸發(fā)ServletContextEvent 事件,該事件由 ServletContextListener 來(lái)處理。在 ServletContextListener 接口中定義了處理ServletContextEvent 事件的兩個(gè)方法。
l contextInitialized(ServletContextEvent sce) :當(dāng)Servlet 容器啟動(dòng)Web 應(yīng)用時(shí)調(diào)用該方法。在調(diào)用完該方法之后,容器再對(duì)Filter 初始化,并且對(duì)那些在Web 應(yīng)用啟動(dòng)時(shí)就需要被初始化的Servlet 進(jìn)行初始化。
l contextDestroyed(ServletContextEvent sce) :當(dāng)Servlet 容器終止Web 應(yīng)用時(shí)調(diào)用該方法。在調(diào)用該方法之前,容器會(huì)先銷(xiāo)毀所有的Servlet 和Filter 過(guò)濾器。
下面通過(guò)兩個(gè)具體的例子來(lái)介紹 ServletContextListener 的用法。
例一:在服務(wù)啟動(dòng)時(shí),將數(shù)據(jù)庫(kù)中的數(shù)據(jù)加載進(jìn)內(nèi)存,并將其賦值給一個(gè)屬性名,其它的 Servlet 就可以通過(guò) getAttribute 進(jìn)行屬性值的訪(fǎng)問(wèn)。有如下兩個(gè)步驟:
1 : ServletContext 對(duì)象是一個(gè)為整個(gè) web 應(yīng)用提供共享的內(nèi)存,任何請(qǐng)求都可以訪(fǎng)問(wèn)里面的內(nèi)容
2 :如何實(shí)現(xiàn)在服務(wù)啟動(dòng)的時(shí)候就動(dòng)態(tài)的加入到里面的內(nèi)容:我們需要做的有:
1 ) 實(shí)現(xiàn) servletContextListerner 接口 并將要共享的通過(guò) setAttribute ( name,data )方法提交到內(nèi)存中去 ;
2 )應(yīng)用項(xiàng)目通過(guò) getAttribute(name) 將數(shù)據(jù)取到 。
package ServletContextTest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import util.ConnectTool;
public class ServletContextLTest implements ServletContextListener{
// 實(shí)現(xiàn)其中的銷(xiāo)毀函數(shù)
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("this is last destroyeed");
}
// 實(shí)現(xiàn)其中的初始化函數(shù),當(dāng)有事件發(fā)生時(shí)即觸發(fā)
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));
}
// 將所取到的值存放到一個(gè)屬性鍵值對(duì)中
sct.setAttribute("dept", depts);
System.out.println("======listener test is beginning=========");
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectTool.releasersc(rs, pstm, connection);
}
}
}
在完成上述編碼后,仍需在 web.xml 中進(jìn)行如下配置,以使得該監(jiān)聽(tīng)器可以起作用。
<listener>
<listener-class>ServletContextTest.ServletContextLTest</listener-class>
</listener>
在完成上述配置后, web 服務(wù)器在啟動(dòng)時(shí),會(huì)直接加載該監(jiān)聽(tīng)器,通過(guò)以下的應(yīng)用程序就可以進(jìn)行數(shù)據(jù)的訪(fǎng)問(wèn)。
package ServletContextTest;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CreateEmployee extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext sct=getServletConfig().getServletContext();
// 從上下文環(huán)境中通過(guò)屬性名獲取屬性值
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();
}
}
例二:書(shū)寫(xiě)一個(gè)類(lèi)用于統(tǒng)計(jì)當(dāng)Web 應(yīng)用啟動(dòng)后,網(wǎng)頁(yè)被客戶(hù)端訪(fǎng)問(wèn)的次數(shù)。如果重新啟動(dòng)Web 應(yīng)用,計(jì)數(shù)器不會(huì)重新從1 開(kāi)始統(tǒng)計(jì)訪(fǎng)問(wèn)次數(shù),而是從上次統(tǒng)計(jì)的結(jié)果上進(jìn)行累加。在實(shí)際應(yīng)用中,往往需要統(tǒng)計(jì)自Web 應(yīng)用被發(fā)布后網(wǎng)頁(yè)被客戶(hù)端訪(fǎng)問(wèn)的次數(shù),這就要求當(dāng)Web 應(yīng)用被終止時(shí),計(jì)數(shù)器的數(shù)值被永久存儲(chǔ)在一個(gè)文件中或者數(shù)據(jù)庫(kù)中,等到Web 應(yīng)用重新啟動(dòng)時(shí),先從文件或數(shù)據(jù)庫(kù)中讀取計(jì)數(shù)器的初始值,然后在此基礎(chǔ)上繼續(xù)計(jì)數(shù)。
向文件中寫(xiě)入或讀取計(jì)數(shù)器的數(shù)值的功能可以由自定義的 MyServletContextListener 類(lèi)來(lái)完成,它具有以下功能:
1 、在 Web 應(yīng)用啟動(dòng)時(shí)從文件中讀取計(jì)數(shù)器的數(shù)值,并把表示計(jì)數(shù)器的 Counter 對(duì)象存放到 Web 應(yīng)用范圍內(nèi)。存放計(jì)數(shù)器的文件的路徑為helloapp/count/count.txt 。
2 、在Web 應(yīng)用終止時(shí)把Web 應(yīng)用范圍內(nèi)的計(jì)數(shù)器的數(shù)值保存到count.txt 文件中。
package ServletContextTest;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyServletContextListener implements ServletContextListener{
public void contextInitialized(ServletContextEvent sce){
System.out.println("helloapp application is Initialized.");
// 獲取 ServletContext 對(duì)象
ServletContext context=sce.getServletContext();
try{
// 從文件中讀取計(jì)數(shù)器的數(shù)值
BufferedReader reader=new BufferedReader(
new InputStreamReader(context.
getResourceAsStream("/count/count.txt")));
int count=Integer.parseInt(reader.readLine());
reader.close();
// 創(chuàng)建計(jì)數(shù)器對(duì)象
Counter counter=new Counter(count);
// 把計(jì)數(shù)器對(duì)象保存到 Web 應(yīng)用范圍
context.setAttribute("counter",counter);
} catch(IOException e) {
e.printStackTrace();
}
}
public void contextDestroyed(ServletContextEvent sce){
System.out.println("helloapp application is Destroyed.");
// 獲取 ServletContext 對(duì)象
ServletContext context=sce.getServletContext();
// 從 Web 應(yīng)用范圍獲得計(jì)數(shù)器對(duì)象
Counter counter=(Counter)context.getAttribute("counter");
if(counter!=null){
try{
// 把計(jì)數(shù)器的數(shù)值寫(xiě)到 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();
}
}
}
}
將用戶(hù)自定義的 MyServletContextListener 監(jiān)聽(tīng)器在 Servlet 容器進(jìn)行注冊(cè), Servlet 容器會(huì)在啟動(dòng)或終止 Web 應(yīng)用時(shí),會(huì)調(diào)用該監(jiān)聽(tīng)器的相關(guān)方法。在 web.xml 文件中, <listener> 元素用于向容器注冊(cè)監(jiān)聽(tīng)器:
<listener>
<listener-class> ServletContextTest .MyServletContextListener<listener-class />
</listener>
通過(guò)上述兩個(gè)例子,即可以非常清楚的了解到 ServletContextListener 接口的使用方法及技巧。 在Container 加載Web 應(yīng)用程序時(shí)(例如啟動(dòng) Container 之后),會(huì)呼叫contextInitialized() ,而當(dāng)容器移除Web 應(yīng)用程序時(shí),會(huì)呼叫contextDestroyed () 方法。 通過(guò) Tomcat 控制臺(tái)的打印結(jié)果的先后順序,會(huì)發(fā)現(xiàn)當(dāng) Web 應(yīng)用啟動(dòng)時(shí),Servlet 容器先調(diào)用contextInitialized() 方法,再調(diào)用lifeInit 的init() 方法;當(dāng)Web 應(yīng)用終止時(shí),Servlet 容器先調(diào)用lifeInit 的destroy() 方法,再調(diào)用contextDestroyed() 方法。由此可見(jiàn),在Web 應(yīng)用的生命周期中,ServletContext 對(duì)象最早被創(chuàng)建,最晚被銷(xiāo)毀。