Session counter with HttpSessionListener and session count variable access

后端 未结 2 1941
走了就别回头了
走了就别回头了 2021-01-29 01:04

I saw an example with session counter in Sun\'s \"Core Servlets and JavaServer Pages vol 2\".
Counter is simply build on HttpSessionListener and increments/decr

2条回答
  •  -上瘾入骨i
    2021-01-29 01:22

    This would be safe.

    public class HttpSessionListenerTest implements HttpSessionListener {
        final private AtomicInteger sessionCount = new AtomicInteger(0);
    
        @Override
        public void sessionCreated(HttpSessionEvent event) {
            sessionCount.incrementAndGet();
        }
    
        @Override
        public void sessionDestroyed(HttpSessionEvent event) {
            sessionCount.decrementAndGet();
        }
    
        public int getTotalSessionCount() {
            return sessionCount.get();
        }
    }
    

提交回复
热议问题