// SessionListener.java
import
java.io.*;
import java.util.*;
import
javax.servlet.http.*;
//監(jiān)聽登錄的整個過程
public class SessionListener implements
HttpSessionBindingListener
{
public String privateInfo=""; //生成監(jiān)聽器的初始化參數(shù)字符串
private String logString=""; //日志記錄字符串
private int
count=0; //登錄人數(shù)計(jì)數(shù)器
public
SessionListener(String info){
this.privateInfo=info;
}
public int getCount(){
return count;
}
public void valueBound(HttpSessionBindingEvent event)
{
count++;
if
(privateInfo.equals("count"))
{
return;
}
try{
Calendar calendar=new
GregorianCalendar();
System.out.println("LOGIN:"+privateInfo+"
TIME:"+calendar.getTime());
logString="\nLOGIN:"+privateInfo+"
TIME:"+calendar.getTime()
+"\n";
for(int i=1;i<1000;i++){
File file=new
File("yeeyoo.log"+i);
if(!(file.exists()))
file.createNewFile(); //如果文件不存在,創(chuàng)建此文件
if(file.length()>1048576) //如果文件大于1M,重新創(chuàng)建一個文件
continue;
FileOutputStream foo=new FileOutputStream
("yeeyoo.log"+i,true);//以append方式打開創(chuàng)建文件
foo.write(logString.getBytes(),0,logString.length()); //寫入日志
字符串
foo.close();
break;//退出
}
}catch(FileNotFoundException e){}
catch(IOException e){}
}
public void valueUnbound(HttpSessionBindingEvent event)
{
count--;
if
(privateInfo.equals("count"))
{
return;
}
try{
Calendar calendar=new
GregorianCalendar();
System.out.println("LOGOUT:"+privateInfo+"
TIME:"+calendar.getTime());
logString="\nLOGOUT:"+privateInfo+" TIME:"+calendar.getTime()
+"\n";
for(int i=1;i<1000;i++){
File file=new File("yeeyoo.log"+i);
if(!(file.exists()))
file.createNewFile(); //如果文件不存在,創(chuàng)建此文件
if(file.length()>1048576) //如果文件大于1M,重新創(chuàng)建一個文件
continue;
FileOutputStream foo=new FileOutputStream
("yeeyoo.log"+i,true);//以append方式打開創(chuàng)建文件
foo.write(logString.getBytes(),0,logString.length()); //寫入日志
字符串
foo.close();
break;//退出
}
}catch(FileNotFoundException e){}
catch(IOException e){}
}
}
登錄日志的實(shí)現(xiàn):
下面再來看看我們的登錄Servlet中使用這個監(jiān)聽器的部分源代碼:
……
HttpSession session = req.getSession (true);
……
////////////////////////////////////////////////////////////////
///////
SessionListener
sessionListener=new SessionListener("
IP:"+req.getRemoteAddr()); //對于每一個會話過程均啟動一個監(jiān)聽器
session.setAttribute("listener",sessionListener); //將監(jiān)聽器植入
HttpSession,這將激發(fā)監(jiān)聽器調(diào)用valueBound方法,從而記錄日志文件
。
////////////////////////////////////////////////////////////////
///////
當(dāng)系統(tǒng)退出登錄時,只需簡單地調(diào)用session.removeAttribute
(“listener”);即可自動調(diào)用監(jiān)聽器的valueUnbound方法。或者,當(dāng)
Session
Time Out的時候也會調(diào)用此方法。
登錄人數(shù)的統(tǒng)計(jì):
ServletContext
session1=getServletConfig().getServletContext
();//取得ServletContext對象實(shí)例
if((SessionListener)session1.getAttribute("listener1")==null)
{
SessionListener sessionListener1=new
SessionListener("count");//
只設(shè)置一次,不同于上面日志文件的記錄每次會話均設(shè)置。即當(dāng)?shù)谝粋€客
戶連接到服務(wù)器時啟動一個全局變量,此后所有的客戶將使用相同的上下
文。
session1.setAttribute("listener1",sessionListener1);//將監(jiān)聽器對
象設(shè)置成ServletContext的屬性,具有全局范圍有效性,即所有的客戶均
可以取得它的實(shí)例。
}
session.setAttribute("listener1",(SessionListener)
session1.getAttribute("listener1"));//取出此全局對象,并且將此對
象綁定到某個會話中,此舉將促使監(jiān)聽器調(diào)用valueBound,計(jì)數(shù)器加一。
在此后的程序中隨時可以用以下代碼取得當(dāng)前的登錄人數(shù):
((SessionListener)session.getAttribute("listener1")).getCount()
getCount()是監(jiān)聽器的一個方法,即取得當(dāng)前計(jì)數(shù)器的值也就是登錄人數(shù)
了。
修改web.xml,增加:
<listener>
<listener-class>SessionListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>SessionListener</servlet-name>
<url-pattern>/servlet/SessionListener</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SessionListener</servlet-name>
<servlet-class>SessionListener</servlet-class>
</servlet>