javaweb監聽器記錄應用的在線人數[從學習到工作(四)]

     1.web.xml的配置

        

 <listener>
    <listener-class>com.test.listener.MySessionListener</listener-class>
</listener>
<session-config>  
    <session-timeout>1</session-timeout>  
</session-config> 
      2.java代碼

     

package com.test.listener;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * 記錄訪問應用的在線人數
 * @author zenglx
 *
 */
public class MySessionListener implements HttpSessionListener,ServletRequestListener{

	 private static String ip = null;
	 private static Map<String,Integer> hm = new HashMap<String,Integer>();
	 private static int activeNumber = 1;
	
	public void sessionCreated(HttpSessionEvent event) {
		HttpSession session = event.getSession();
		ServletContext context = session.getServletContext();
		activeNumber = (Integer) (context.getAttribute(ip)==null?1:context.getAttribute(ip));
		if(hm.isEmpty()) {
			hm.put(ip, activeNumber);
		} else {
		     Set<String> keys = hm.keySet();
		     //判斷ip是否在map中
		     boolean isContans = keys.contains(ip);
		     if(isContans) {
		    	 activeNumber++;
		    	 //將ip和session連接數放入map中,在key相同的情況下,Map中只有一個鍵值對
		    	 hm.put(ip, activeNumber);
		     } else {
		    	 hm.put(ip, activeNumber);
		     }
		}
		context.setAttribute(ip, activeNumber);
		context.setAttribute(session.getId(),ip);
		System.out.println("sessionCreated:"+"在線人數:"+hm.size()+"---"+ip+"的session連接數:"+activeNumber+":"+session.getId());
		
		//將在線人數放入context中
		context.setAttribute("onLineNumber", hm.size());
	}

	public void sessionDestroyed(HttpSessionEvent event) {
		HttpSession session = event.getSession();
		ServletContext context = session.getServletContext();
		int active = (Integer) context.getAttribute((String) context.getAttribute(session.getId()));
		if(active == 1) {
			context.setAttribute((String) context.getAttribute(session.getId()), active);
			//map中移除session失效的ip
			hm.remove((String) context.getAttribute(session.getId()));
			System.out.println("sessionDestroy:"+"在線人數:"+hm.size()+"---"+(String) context.getAttribute(session.getId())+"的session連接數:0:"+session.getId());
			//context中移除放入其中的ip地址和 sessionid
			context.removeAttribute((String) context.getAttribute(session.getId()));
			context.removeAttribute(session.getId());
		} else {
			context.setAttribute((String) context.getAttribute(session.getId()), active-1);
			System.out.println("sessionDestroy:"+"在線人數:"+hm.size()+"---"+(String) context.getAttribute(session.getId())+"的session連接數:"+(active-1)+":"+session.getId());
			//context中移除放入其中的 sessionid
			context.removeAttribute(session.getId());
		}
		//將在線人數放入context中
		context.setAttribute("onLineNumber", hm.size());
	}

	public void requestDestroyed(ServletRequestEvent arg0) {
		
	}

	public void requestInitialized(ServletRequestEvent event) {
		ServletRequest request = event.getServletRequest();
		HttpServletRequest req = (HttpServletRequest) request;
		ip = req.getRemoteAddr();
	}

}

總結:記錄在某一時間應用訪問的在線人數,可以看到同一IP下有多少session連接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章