编写函数

meng___xuan 2003-03-06 11:17:17
怎样用java定义一个静态变量,统计在线的人数。
...全文
99 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
peacock_king 2003-03-06
  • 打赏
  • 举报
回复
你测试的时候,会有一点问题,因为一个session停用后的默认destroy时间是30分钟,当一个用户停用后,要再过30分钟后,activeSessions才会减一。

时间可在web.xml中调整,为了方便测试,可以设为1分钟。
peacock_king 2003-03-06
  • 打赏
  • 举报
回复
/*
* SessionCount.java
*
* Created on 2003年1月22日, 下午9:01
*/

import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;

/**
*
* @author Administrator
* @version
*/

public class SessionCount implements HttpSessionListener {

// The session listener model is similar to the context listener model.
// Any listener interested in observing the http session lifecycle
// should implement the HttpSessionListener interface.

// The methods accept an HttpSessionEvent instance with a getSession() method
// to return the session being created or destroyed.
private static int activeSessions = 0;
public static int getActiveSessions() {
return activeSessions;
}
public void sessionCreated(HttpSessionEvent e) {
// Called when a session is created
activeSessions++;
}

public void sessionDestroyed(HttpSessionEvent e) {
// Called when a session is destroyed (invalidated)
if(activeSessions > 0) activeSessions--;
}
}

把这个servlet设为随服务器启动,在jsp中调用时:
<%out.write(SessionCount.getActiveSessions());%>
Jin901 2003-03-06
  • 打赏
  • 举报
回复
public class 类名{
static int count = 0;
public 类名(){ //构造函数
count = count + 1;
}
...
}
kofwr 2003-03-06
  • 打赏
  • 举报
回复
最简单的,jsp中写个Application范围的变量,每调用一次++一次..

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧