如题,项目启动的时候调用了三次我自己写的OnlineCountListener类,
我用来统计在线人数的。可是我服务一启动就会调用三次这个sessionCreated方法。
我的监听器package com.looveh.demo1;import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;@WebListener
public class OnlineCountListener implements HttpSessionListener { public int count = 0; @Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
System.out.println("session created");
count++;
httpSessionEvent.getSession().getServletContext().setAttribute("count",count);
} @Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
System.out.println("session destroyed");
count--;
httpSessionEvent.getSession().getServletContext().setAttribute("count",count);
}
}
HTML
<%@page language="java" pageEncoding="utf-8" contentType="text/html; charset=UTF-8" %>
<html>
<body>
<h2>Hello World!</h2>
在线人数:<%=application.getAttribute("count")%>
</body>
</html>
web.xml<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app>
<display-name>Archetype Created Web Application</display-name> <listener>
<listener-class>com.looveh.demo1.OnlineCountListener</listener-class>
</listener>
</web-app>启动的时候就直接是3了,销毁什么的都是正常的!
我用来统计在线人数的。可是我服务一启动就会调用三次这个sessionCreated方法。
我的监听器package com.looveh.demo1;import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;@WebListener
public class OnlineCountListener implements HttpSessionListener { public int count = 0; @Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
System.out.println("session created");
count++;
httpSessionEvent.getSession().getServletContext().setAttribute("count",count);
} @Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
System.out.println("session destroyed");
count--;
httpSessionEvent.getSession().getServletContext().setAttribute("count",count);
}
}
HTML
<%@page language="java" pageEncoding="utf-8" contentType="text/html; charset=UTF-8" %>
<html>
<body>
<h2>Hello World!</h2>
在线人数:<%=application.getAttribute("count")%>
</body>
</html>
web.xml<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app>
<display-name>Archetype Created Web Application</display-name> <listener>
<listener-class>com.looveh.demo1.OnlineCountListener</listener-class>
</listener>
</web-app>启动的时候就直接是3了,销毁什么的都是正常的!
解决方案 »
- 别人给我一个SSH项目 我该怎么运行起来
- 谁晓得 struts2.2 和 guice 3.0 怎么整合吗?
- 正则表达式^([^.]*)$
- 解析带文档注释的XML
- struts1.2 spring hibernate 执行自定义方法问题 在线等
- java timestamp类型存入oracle date字段问题
- 请教 selvlet通过 Local接口调用ejb ,学要怎样作才正确啊
- 编译无状态会话Bean时出现错误:Spaces in the temporary directory path may cause WebLogic APPC utility to produce fatal compile er
- 利用用户名和密码,怎么在后台登陆获取coocike
- jsp获取servlet的println并且显示在文本框中?
- 大佬们,天猫淘宝的单点登录是怎么实现的?
- idea启动时报出这个异常
你是打成war包放到tomcat下运行吗?
你是打成war包放到tomcat下运行吗?嗯嗯, 打包放 webapps 下是正常的,我也是醉了. 搞不懂为啥.