我写的一个在先人数统计,(有代码)为什么注销没有用??

weicairong 2004-09-30 08:16:56
实现人数统计的类的代码:(OnLineCount.java)
package OAWei;

import javax.servlet.*;
import javax.servlet.http.*;

public class OnLineCount implements HttpSessionListener{
private static int currentCount=0;

public OnLineCount(){}
public void sessionCreated(HttpSessionEvent se){
currentCount++;
}

public void sessionDestroyed(HttpSessionEvent se){
if(currentCount>0)
currentCount--;
}
public static int getCurrentCount(){
return currentCount;
}
}


显示人数的代码:(OnLineCount.jsp)
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" %>
<%@ page import="OAWei.OnLineCount" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>统计在线人数</title>
</head>

<body>
<%
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","No-cache");
response.setDateHeader("Expires",0);
session.setAttribute("name","weicairong");
OnLineCount count=new OnLineCount();
out.print(count.getCurrentCount());



%>
<a href="logout.jsp">注销</a>
</body>
</html>
注销的代码:(logout.jsp)
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>

<body>
<%
session.removeAttribute("name");
session.invalidate();

response.sendRedirect("OnLineCount.jsp");
%>
</body>
</html>


...全文
166 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
angelheart 2004-10-01
  • 打赏
  • 举报
回复
我只是直接修改了一下你的代码,实现最简单的人数统计
对了,如果
se.getSession().getServletContext().setAttribute("count",currentCount);
有编译错误的话就改为
se.getSession().getServletContext().setAttribute("count",new Integer(currentCount));

这个是我以前回的一个帖子

http://community.csdn.net/Expert/topic/3366/3366726.xml?temp=.6474726
weicairong 2004-10-01
  • 打赏
  • 举报
回复
我是菜鸟,不上网搞点代码能行吗?
weicairong 2004-10-01
  • 打赏
  • 举报
回复
这段代码是网上的,但是不copy的,是我自己敲的,HttpSessionListener的用法我是看了
《servelet 和jsp权威指南》的
angelheart(大四了) :你的代码肯定可以用吗??
angelheart 2004-09-30
  • 打赏
  • 举报
回复
<%
session.removeAttribute("name");
session.invalidate();

response.sendRedirect("OnLineCount.jsp");
%>

这代码有问题,用了response必然存在session的吧~
angelheart 2004-09-30
  • 打赏
  • 举报
回复
感觉代码最好还是自己写好~~不要一味的在网上复制,粘贴
angelheart 2004-09-30
  • 打赏
  • 举报
回复
楼主,你需要把COUNT放入servletcontext里面啊~~
package OAWei;

import javax.servlet.*;
import javax.servlet.http.*;

public class OnLineCount implements HttpSessionListener{
private static int currentCount=0;

public OnLineCount(){}
public void sessionCreated(HttpSessionEvent se){
currentCount++;

se.getSession().getServletContext().setAttribute("count",currentCount);
}

public void sessionDestroyed(HttpSessionEvent se){
if(currentCount>0) {
currentCount--;
se.getSession().getServletContext().setAttribute("count",currentCount);
}
}
public static int getCurrentCount(){
return currentCount;
}
}

在web.xml里面部署
<listener>
<listener-class>OAWei.OnLineCount</listener-class>
</listener>

JSP页面
<%
out.print(application.getAttribute("count"));
%>
jndszl 2004-09-30
  • 打赏
  • 举报
回复
不是偏见,不信你在GOOGLE上找一下,JSP在线统计人数。全是和楼主类似的文章,说什么,真正实现在线统计等等。其实你可以试一下,全都是只能增加人数,不能减少人数,统计的数据不准,要这玩意还有什么用。还不如不用。
angelheart 2004-09-30
  • 打赏
  • 举报
回复
晕~~楼上得太偏见了
禽兽v5 2004-09-30
  • 打赏
  • 举报
回复
不熟。看到楼上的发言,深有同感。
jndszl 2004-09-30
  • 打赏
  • 举报
回复
你在网上查一下,有很多人都是这样写的,都说是实现用户注销,全是P话,我也试过基本不管用。看来网上这些文章全是抄来抄去的

81,122

社区成员

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

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