// SessionListener.java
import
java.io.*;
import java.util.*;
import
javax.servlet.http.*;
//監聽登錄的整個過程
public class SessionListener implements
HttpSessionBindingListener
{
public String privateInfo=""; //生成監聽器的初始化參數字符串
private String logString=""; //日志記錄字符串
private int
count=0; //登錄人數計數器
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(); //如果文件不存在,創建此文件
if(file.length()>1048576) //如果文件大于1M,重新創建一個文件
continue;
FileOutputStream foo=new FileOutputStream
("yeeyoo.log"+i,true);//以append方式打開創建文件
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(); //如果文件不存在,創建此文件
if(file.length()>1048576) //如果文件大于1M,重新創建一個文件
continue;
FileOutputStream foo=new FileOutputStream
("yeeyoo.log"+i,true);//以append方式打開創建文件
foo.write(logString.getBytes(),0,logString.length()); //寫入日志
字符串
foo.close();
break;//退出
}
}catch(FileNotFoundException e){}
catch(IOException e){}
}
}
登錄日志的實現:
下面再來看看我們的登錄Servlet中使用這個監聽器的部分源代碼:
……
HttpSession session = req.getSession (true);
……
////////////////////////////////////////////////////////////////
///////
SessionListener
sessionListener=new SessionListener("
IP:"+req.getRemoteAddr()); //對于每一個會話過程均啟動一個監聽器
session.setAttribute("listener",sessionListener); //將監聽器植入
HttpSession,這將激發監聽器調用valueBound方法,從而記錄日志文件
。
////////////////////////////////////////////////////////////////
///////
當系統退出登錄時,只需簡單地調用session.removeAttribute
(“listener”);即可自動調用監聽器的valueUnbound方法。或者,當
Session
Time Out的時候也會調用此方法。
登錄人數的統計:
ServletContext
session1=getServletConfig().getServletContext
();//取得ServletContext對象實例
if((SessionListener)session1.getAttribute("listener1")==null)
{
SessionListener sessionListener1=new
SessionListener("count");//
只設置一次,不同于上面日志文件的記錄每次會話均設置。即當第一個客
戶連接到服務器時啟動一個全局變量,此后所有的客戶將使用相同的上下
文。
session1.setAttribute("listener1",sessionListener1);//將監聽器對
象設置成ServletContext的屬性,具有全局范圍有效性,即所有的客戶均
可以取得它的實例。
}
session.setAttribute("listener1",(SessionListener)
session1.getAttribute("listener1"));//取出此全局對象,并且將此對
象綁定到某個會話中,此舉將促使監聽器調用valueBound,計數器加一。
在此后的程序中隨時可以用以下代碼取得當前的登錄人數:
((SessionListener)session.getAttribute("listener1")).getCount()
getCount()是監聽器的一個方法,即取得當前計數器的值也就是登錄人數
了。
修改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>