使用HttpSessionListener监听器实战

HttpSessionListener监听器

定义监听器

package lee; 
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;import java.util.*;
@WebListener
public class OnlineListener
    implements HttpSessionListener
{
    // 当用户与服务器之间开始session时触发该方法
    public void sessionCreated(HttpSessionEvent se)
    {
        HttpSession session = se.getSession();
        ServletContext application = session.getServletContext();
        // 获取session ID
        String sessionId = session.getId();
        // 如果是一次新的会话
        if (session.isNew())
        {
            String user = (String)session.getAttribute("user");
            // 未登录用户当游客处理
            user = (user == null) ? "游客" : user;
            Map online = (Map)
                application.getAttribute("online");
            if (online == null)
            {
                online = new Hashtable();
            }
            // 将用户在线信息放入Map中
            online.put(sessionId , user);
            application.setAttribute("online" , online);
        }
    }
    // 当用户与服务器之间session断开时触发该方法
    public void sessionDestroyed(HttpSessionEvent se)
    {
        HttpSession session = se.getSession();
        ServletContext application = session.getServletContext();
        String sessionId = session.getId();
        Map online = (Map)
            application.getAttribute("online");
        if (online != null)
        {
            // 删除该用户的在线信息
            online.remove(sessionId);
        }
        application.setAttribute("online" , online);
    }
}

测试JSP

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ page import="java.util.*" %>



     用户在线信息 
    


在线用户:

<%
Map online = (Map)application
    .getAttribute("online");
for (String sessionId : online.keySet())
{%>

<%}%>

测试结果

使用HttpSessionListener监听器实战_第1张图片

HttpSessionListener监听器应用场景

使用HttpSessionListener监听器实战_第2张图片

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

实现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被销毁"); 
    } 
}

登陆界面去创建HttpSessionListenter

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

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

登出销毁HttpSessionListenter

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

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

已退出

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

web.xml中配置监听



    
        MyHttpSessionListener
    
    
        myServletContextListener
    

统计人数实在最大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} 退出

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。 

你可能感兴趣的:(使用HttpSessionListener监听器实战)

<%=sessionId%> <%=online.get(sessionId)%>