日期:2014-05-17  浏览次数:20733 次

在线用户的统计问题
本人初学java,在试验网页访问人数统计,本意通过session来计算访问人数和在线人数,但是在用户点击网页关闭按钮的时候,并没有调用session的销毁方法,请教如何解决。 


------解决方案--------------------
http://topic.csdn.net/t/20010528/21/138139.html


------解决方案--------------------
httpsession事件监听器
Java code
public class OnlineListener implementss HttpSessionListener
private int onlineCount;
public OnlineListener()
{onlineCount=0;
}
public void sessionCreated(HttpSessionEvent sessionEvent)
{
onlineCount++;
sessionEvent.getSession().getServletContext().setAttribute("online",new Integer(onlineCount));
}
public void sessionDestroyed(HttpSessionEvent sessionEvent)
{
onlineCount--;
sessionEvent.getSession().getServletContext().setAtrribute("online",new Integer(onlineCount));
}
}

------解决方案--------------------
我们刚学JSP,学了个application的内置对象……可以用它实现在线用户统计,好像,你可以看看,我也是刚学的,一起讨论讨论
index.jsp页面如下:
<%
Integer acount;
acount = (Interger)application.getAttrubute("acounter");
if(acount==null){
acount = new Integer(1);
}else{
acount = new Integer(requset.intValue()+1);
}
application.setAttribute("acounter","acount");
%>
<table>
<tr>
<th>在线用户为</th>
<Td><%=application.getAttribute("acounter")%></Td>
</tr>
</table>