HttpSessionListener监听器应用场景

HttpSessionListener监听器应用场景_第1张图片

应用场景:用来统计当前在线人数

1.实现HttpSessionListener

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MyHttpSessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        System.out.println("httpsession被创建");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        System.out.println("httpsession被销毁");

    }

}

 

2.登陆界面去创建HttpSessionListenter

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

  
    $Title$
  
  
  <%
    <%--    创建HttpSessionListenter--%>
    request.getSession();
  %>

  

3.登出销毁HttpSessionListenter

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

  
    $Title$
  
  
  <%
<%--    销毁HttpSessionListener--%>
    request.getSession().invalidate();
  %>

已退出

实现统计登陆人数(多线程并发)

web.xml中配置监听

 



    
        MyHttpSessionListener
    

    
        myServletContextListener
    

1.统计人数实在最大ServletContextListener这个域当中,因为HttpSessionListener监听器只在当前会话中有效

(1)创建ServletContextListener监听器并设置初始值为0

 

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class myServletContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext sc = servletContextEvent.getServletContext();
        sc.setAttribute("count", 0);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

2)变更在线人数

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MyHttpSessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        System.out.println("httpsession被创建");
        countPersion( httpSessionEvent.getSession().getServletContext(), true);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        System.out.println("httpsession被销毁");

        countPersion(httpSessionEvent.getSession().getServletContext(), false);
    }


    /*
    * 变更在线的人数
    * */
    public void countPersion(ServletContext sc, boolean isAdd) {
        // 为了防止多线程并发问题加锁
        synchronized (sc) {
            // 获得当前的在线人数
            Integer count = (Integer) sc.getAttribute("count");
            if(isAdd) {
                sc.setAttribute("count", ++count);
            } else  {
                sc.setAttribute("count", --count);
            }
        }
    }
}

(3)前端页面上去获取显示

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

  
    $Title$
  
  
  <%
<%--    创建HttpSessionListenter--%>
    request.getSession();
  %>
  

欢迎登陆


当前的在线人数 ${count} 退出

 

你可能感兴趣的:(java)