怎么在一个session结束时得知呢

zyzyandzyzy 2003-10-19 09:58:21
我写一个聊天室,想在一个session结束时把这个人的id从bean中删除,但不知道怎么样才能在用户关闭窗口结束session时被告知
...全文
66 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wellsoon 2003-10-23
  • 打赏
  • 举报
回复



servlet的 listener。

lovemilla 2003-10-23
  • 打赏
  • 举报
回复
mark,楼主,我只能帮你顶。
只有一个想法,呵呵,瞎想:一个计时器,设好循环时间,检查session.kill(),我假设的啊,如果true,则如何如何。
zyzyandzyzy 2003-10-23
  • 打赏
  • 举报
回复
不是吧,请大家不吝指教
wellsoon 2003-10-23
  • 打赏
  • 举报
回复
2.用listener实现在线人数统计.

package beanservlettest;//除了其中三行代码,其余均由Jbuilder自动生成

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class sessioncount extends HttpServlet implements ServletContextListener, ServletContextAttributeListener, HttpSessionListener, HttpSessionAttributeListener {
private static int activeSessions = 0;
public void contextInitialized(ServletContextEvent sce) {

}

public void contextDestroyed(ServletContextEvent sce) {

}
public void attributeAdded(ServletContextAttributeEvent scab) {

}
public void attributeRemoved(ServletContextAttributeEvent scab) {

}
public void attributeReplaced(ServletContextAttributeEvent scab) {

}
public void sessionCreated(HttpSessionEvent se) {
activeSessions++;//每创建一个新的session就 加1
}
public void sessionDestroyed(HttpSessionEvent se) {
if(activeSessions > 0) //每销毁一个新的session就 减1
activeSessions--;
}
public void attributeAdded(HttpSessionBindingEvent se) {

}
public void attributeRemoved(HttpSessionBindingEvent se) {
}

public void attributeReplaced(HttpSessionBindingEvent se) {
}
public static int getActiveSessions() {
return activeSessions;
}

}


web.xml里的配置如下:

<listener>
<listener-class>beanservlettest.sessioncount</listener-class>
</listener>

jsp测试页面:

<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="beanservlettest.sessioncount" %>

<html>
<head>
<title>
sc
</title>
</head>
<body bgcolor="#ffffff">
<h1>
JBuilder Generated JSP
</h1>
当前在线人数有<%=sessioncount.getActiveSessions()%>人。
</body>
</html>
<a href= "logout.jsp">注销</a>


网页连接:

http://expert.csdn.net/Expert/topic/1788/1788525.xml?temp=.2403833
http://www.chinaunix.net/bbsjh/14/397.html

TonyTonyQ 2003-10-23
  • 打赏
  • 举报
回复
public interface HttpSessionListener
extends java.util.EventListener
Implementations of this interface may are notified of changes to the list of active sessions in a web application. To recieve notification events, the implementation class must be configured in the deployment descriptor for the web application.

public class MyServlet extends HttpServlet implements HttpSessionListener
没有试过 我想应该是这样的吧。试试看。
zyzyandzyzy 2003-10-20
  • 打赏
  • 举报
回复
up

81,090

社区成员

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

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