首先编写一个模型类[如果还需要其他信息可增加类属性]:
代码:
package com.bupticet.listener; public class OnlineModel{ private String userName; private String ipAddress; private String loadTime; private String address; /** * OnlineModel */ public OnlineModel() { } public OnlineModel(String name,String ip, String time,String address){ this.userName = name; this.ipAddress = ip; this.loadTime = time; this.address = address; } public String getIpAddress() { return ipAddress; } public String getAddress() { return address; } public String getLoadTime() { return loadTime; } public void setUserName(String userName) { this.userName = userName; } public void setIpAddress(String ipAddress) { this.ipAddress = ipAddress; } public void setAddress(String address) { this.address = address; } public void setLoadTime(String loadTime) { this.loadTime = loadTime; } public String getUserName() { return userName; } public String toString(){ return this.userName+" "+this.ipAddress+" "+this.loadTime+" "+this.address+"\n\n"+"<br>"; } }
再写一个session监听类,实现HttpSessionListener接口:
代码:
package com.bupticet.listener; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class OnlineList implements HttpSessionAttributeListener{ private static List list=new ArrayList(); public void attributeAdded(HttpSessionBindingEvent se){ if("online".equals(se.getName())) { list.add((OnlineModel)(se.getValue())); } } public void attributeRemoved(HttpSessionBindingEvent se){ if("online".equals(se.getName())){ list.remove((OnlineModel)(se.getValue())); } } public void attributeReplaced(HttpSessionBindingEvent se){} public static List getList(){ return(list); } }
然后在web.xml中声明这个监听器:
代码:
<listener> <listener-class> com.bupticet.listener.OnlineList </listener-class> </listener>
最后在登陆的servlet或者页面中加入如下代码[请做适当修改]:
代码:
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy年MM月dd日HH:mm:ss"); String time = sdf.format(java.util.Calendar.getInstance().getTime()); String[] address = IPLocalizer.search(request.getRemoteAddr()); session.setAttribute("online",new OnlineModel(userName,request.getRemoteAddr(),time,address[0]+address[1]));
这样就较为完美的实现了一个在线统计系统
 
显示在线统计:


PHP 代码:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <
tr class="tableHead" align=center>
    <
td width='5%' nowrap>序号</a></td>
    <
td width="0%">|</td>
    <
td width='18%'>姓名</a></td>
    <
td width="0%">|</td>
    <
td width='20%'>IP</a></td>
    <
td width="0%">|</td>
    <
td width='30%'>登陆时间</a></td>
    <
td width="0%">|</td>
        <
td width='27%'>来自于</a></td>
    <
td width="0%">|</td>
  </
tr>
<%
java.util.List list=com.bupticet.listener.OnlineList.getList();
int onlineNumber = list.size();
int onlinePageCount 0;
if(
onlineNumber%pagesize==0){
    
onlinePageCount onlineNumber/pagesize;
}else{
    
onlinePageCount onlineNumber/pagesize+1;
}
String str "";
String trbgcolor=Page_trbgcolor;

int i = (pageNum-1)*pagesize;
int j 0;
for(;
i<onlineNumber&&j<pagesize;i++,j++){
    
com.bupticet.listener.OnlineModel om = (com.bupticet.listener.OnlineModel)(list.get(i));
    
trbgcolor=(trbgcolor==Page_trbgcolor)?"#FFFFFF":Page_trbgcolor;
     
str+=" <tr  height='21' align='center' bgcolor='"+trbgcolor+"'>"+"\n";
       
str+="<td nowrap><font color='"+Page_warncolor+"'>"+(i+1)+"</font></td>"+"\n";
       
str+="<td width='0%'></td>"+"\n";
       
str+="<td nowrap><font color='"+Page_warncolor+"'>"+om.getUserName()+"</font></td>"+"\n";
       
str+="<td width='0%'></td>"+"\n";
       
str+="<td nowrap><font color='"+Page_warncolor+"'>"+om.getIpAddress()+"</font></td>"+"\n";
       
str+="<td width='0%'></td>"+"\n";
       
str+="<td nowrap><font color='"+Page_warncolor+"'>"+om.getLoadTime()+"</font></td>"+"\n";
       
str+="<td width='0%'></td>"+"\n";
       
str+="<td nowrap><font color='"+Page_warncolor+"'>"+om.getAddress()+"</font></td>"+"\n";
           
str+="<td width='0%'></td></tr>"+"\n";
}
for(;
j<pagesize;j++){
        
trbgcolor=(trbgcolor==Page_trbgcolor)?"#FFFFFF":Page_trbgcolor;
     
str+=" <tr  height='21' align='center' bgcolor='"+trbgcolor+"'>"+"\n";
       
str+="<td nowrap><font color='"+Page_warncolor+"'>"+"&nbsp;"+"</font></td>"+"\n";
       
str+="<td width='0%'></td>"+"\n";
       
str+="<td nowrap><font color='"+Page_warncolor+"'>"+"&nbsp;"+"</font></td>"+"\n";
       
str+="<td width='0%'></td>"+"\n";
       
str+="<td nowrap><font color='"+Page_warncolor+"'>"+"&nbsp;"+"</font></td>"+"\n";
       
str+="<td width='0%'></td>"+"\n";
       
str+="<td nowrap><font color='"+Page_warncolor+"'>"+"&nbsp;"+"</font></td>"+"\n";
       
str+="<td width='0%'></td>"+"\n";
       
str+="<td nowrap><font color='"+Page_warncolor+"'>"+"&nbsp;"+"</font></td>"+"\n";
           
str+="<td width='0%'></td></tr>"+"\n";

}
%>
</
table
  用session监听实现在线统计_Java