JavaでHttpSessionListenerを利用してオンライン人数統計の方法

1.web.xmlの設定
<listener>
<listener-class>com.startnews24.store.common.SessionListenerStartnews24</listener-class>
</listener>
<session-config>
<session-timeout>5</session-timeout>
</session-config>

2.javaコード下記
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class SessionListenerStartnews24 implements HttpSessionListener {
private long onlineCnt;
public void sessionCreated(HttpSessionEvent event) {
this.onlineCnt=this.onlineCnt+1;
event.getSession().getServletContext().setAttribute(“onlineCnt”, onlineCnt);
}
public void sessionDestroyed(HttpSessionEvent event) {
this.onlineCnt=this.onlineCnt-1;
event.getSession().getServletContext().setAttribute(“onlineCnt”, onlineCnt);
}

}

Comments are closed.