读书人

在线用户的统计有关问题

发布时间: 2012-03-21 13:33:15 作者: rapoo

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


[解决办法]
http://topic.csdn.net/t/20010528/21/138139.html


[解决办法]
httpsession事件监听器

Java code
public class OnlineListener implementss HttpSessionListenerprivate 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>

读书人网 >Java Web开发

热点推荐