ServletContext 被 Servlet 程序用來與 Web 容器通信。例如寫日志,轉(zhuǎn)發(fā)請(qǐng)求。每一個(gè) Web 應(yīng)用程序含有一個(gè)Context,被Web應(yīng)用內(nèi)的各個(gè)程序共享。因?yàn)镃ontext可以用來保存資源并且共享,所以我所知道的 ServletContext 的最大應(yīng)用是Web緩存----把不經(jīng)常更改的內(nèi)容讀入內(nèi)存,所以服務(wù)器響應(yīng)請(qǐng)求的時(shí)候就不需要進(jìn)行慢速的磁盤I/O了。ServletContextListener 是 ServletContext 的監(jiān)聽者,如果 ServletContext 發(fā)生變化,如服務(wù)器啟動(dòng)時(shí) ServletContext 被創(chuàng)建,服務(wù)器關(guān)閉時(shí) ServletContext 將要被銷毀。在JSP文件中,application 是 ServletContext 的實(shí)例,由JSP容器默認(rèn)創(chuàng)建。Servlet 中調(diào)用 getServletContext()方法得到 ServletContext 的實(shí)例。
我們使用緩存的思路大概是:
服務(wù)器啟動(dòng)時(shí),ServletContextListener 的 contextInitialized()方法被調(diào)用,所以在里面創(chuàng)建好緩存。可以從文件中或者從數(shù)據(jù)庫(kù)中讀取取緩存內(nèi)容生成類,用 ervletContext.setAttribute()方法將緩存類保存在 ServletContext 的實(shí)例中。
程序使用 ServletContext.getAttribute()讀取緩存。如果是 JSP,使用a pplication.getAttribute()。如果是 Servlet,使用 getServletContext().getAttribute()。如果緩存發(fā)生變化(如訪問計(jì)數(shù)),你可以同時(shí)更改緩存和文件/數(shù)據(jù)庫(kù)?;蛘吣愕?變化積累到一定程序再保存,也可以在下一步保存。
服務(wù)器將要關(guān)閉時(shí),ServletContextListener 的 contextDestroyed()方法被調(diào)用,所以在里面保存緩存的更改。將更改后的緩存保存回文件或者數(shù)據(jù)庫(kù),更新原來的內(nèi)容。
import User; //my own class
import DatabaseManager; // my own class
import javax.servlet.ServletContext;
import javax.servlet.ServletContextListener;
public class MyContextListener
implements ServletContextListener {
private ServletContext context = null;
public void contextInitialized(ServletContextEvent event) {
context = event.getServletContext();
User user = DatabaseManager.getUserById(1);
context.setAttribute("user1", user);
}
public void contextDestroyed(ServletContextEvent event) {
User user = (User)context.getAttribute("user1"); DatabaseManager.updateUserData(user);
this.context = null;
}
}
布署 ServletContextListener
你實(shí)現(xiàn)(implements)了 ServletContextListener 編譯后,把它放在正確的WEB-INF/classes目錄下,更改WEB-INF目錄下的 web.xml文件,在web-app節(jié)點(diǎn)里添加
MyServletContextListener
下面的例子很好?。?!
在 Servlet API 中有一個(gè) ServletContextListener 接口,它能夠監(jiān)聽 ServletContext 對(duì)象的生命周期,實(shí)際上就是監(jiān)聽 Web 應(yīng)用的生命周期。當(dāng)Servlet 容器啟動(dòng)或終止Web 應(yīng)用時(shí),會(huì)觸發(fā)ServletContextEvent 事件,該事件由ServletContextListener 來處理。在 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ì)先銷毀所有的Servlet 和Filter 過濾器。
下面通過兩個(gè)具體的例子來介紹 ServletContextListener 的用法。
例一:在服務(wù)啟動(dòng)時(shí),將數(shù)據(jù)庫(kù)中的數(shù)據(jù)加載進(jìn)內(nèi)存,并將其賦值給一個(gè)屬性名,其它的 Servlet 就可以通過 getAttribute 進(jìn)行屬性值的訪問。有如下兩個(gè)步驟:1 : ServletContext 對(duì)象是一個(gè)為整個(gè) web 應(yīng)用提供共享的內(nèi)存,任何請(qǐng)求都可以訪問里面的內(nèi)容
2 :如何實(shí)現(xiàn)在服務(wù)啟動(dòng)的時(shí)候就動(dòng)態(tài)的加入到里面的內(nèi)容:我們需要做的有:
1 ) 實(shí)現(xiàn) servletContextListerner 接口 并將要通過 setAttribute ( name,data )方法提交到內(nèi)存中去 ;
2 )應(yīng)用項(xiàng)目通過 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)其中的銷毀函數(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 depts=new HashMap();
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)聽器可以起作用。
ServletContextTest.ServletContextLTest
在完成上述配置后, web 服務(wù)器在啟動(dòng)時(shí),會(huì)直接加載該監(jiān)聽器,通過以下的應(yīng)用程序就可以進(jìn)行數(shù)據(jù)的訪問。
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)境中通過屬性名獲取屬性值
Map dept(Map)sct.getAttribute("dept");
Set key=dept.keySet();
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.println("");
out.println("");
out.println("");
out.println("");
");
out.println("username:");
out.println("");
");
out.println("
out.println("");
");
out.println("city:");
out.println("");
");
out.println("
for(Integer i:key){
out.println(""+dept.get(i)+"");
}
out.println("");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("");
out.flush();
}
}
例二:書寫一個(gè)類用于統(tǒng)計(jì)當(dāng)Web 應(yīng)用啟動(dòng)后,網(wǎng)頁被客戶端訪問的次數(shù)。如果重新啟動(dòng)Web 應(yīng)用,計(jì)數(shù)器不會(huì)重新從1 開始統(tǒng)計(jì)訪問次數(shù),而是從上次統(tǒng)計(jì)的結(jié)果上進(jìn)行累加。在實(shí)際應(yīng)用中,往往需要統(tǒng)計(jì)自Web 應(yīng)用被發(fā)布后網(wǎng)頁被客戶端訪問的次數(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ù)。
向文件中寫入或讀取計(jì)數(shù)器的數(shù)值的功能可以由自定義的 MyServletContextListener 類來完成,它具有以下功能:
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;
importjavax.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ù)值寫到 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 監(jiān)聽器在 Servlet 容器進(jìn)行注冊(cè), Servlet 容器會(huì)在啟動(dòng)或終止 Web 應(yīng)用時(shí),會(huì)調(diào)用該監(jiān)聽器的相關(guān)方法。在 web.xml 文件中, 元素用于向容器注冊(cè)監(jiān)聽器:
ServletContextTest .MyServletContextListener
注意:中要寫出類的包名,比如下面的寫法(包名標(biāo)識(shí)了類的路徑)
com.efarm360.action.StartupAction
通過上述兩個(gè)例子,即可以非常清楚的了解到 ServletContextListener 接口的使用方法及技巧。在Container 加載Web 應(yīng)用程序時(shí)(例如啟動(dòng) Container 之后),會(huì)呼叫contextInitialized() ,而當(dāng)容器移除Web 應(yīng)用程序時(shí),會(huì)呼叫contextDestroyed () 方法。 通過 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() 方法。由此可見,在Web 應(yīng)用的生命周期中,ServletContext 對(duì)象最早被創(chuàng)建,最晚被銷毀。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 技術(shù)分享:www.kaige123.com