统计在线已登录用户数量并获取客户端IP(涉及HttpSessionListener监听)

本文介绍了一个利用HttpSessionListener监听实现统计在线已登录用户数量,并获取客户端IP的方法。通过监听session创建和销毁,结合应用上下文管理用户session,确保在线人数的准确性。在用户登录成功和退出时,更新application对象中保存的session信息,从而实时跟踪在线用户。
摘要由CSDN通过智能技术生成

 

 

项目需要做一个统计在线登陆用户数量的功能。

首先我想到的是写HttpSessionListener监听,根据session来确定用户数量。

 

 

步骤:

web.xml配置HttpSessionListener监听

<listener>
		<listener-class>org.hd.base.listener.HDSessionListener</listener-class>
	</listener>


监听的类:

package org.hd.base.listener;

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

import org.apache.log4j.Logger;
import org.hd.util.ConstantDifinitionUtil;


public class HDSessionListener implements HttpSessionListener {
	private static final Logger logger = Logger.getLogger(HDSessionListener.class.getName());
	
	public void sessionCreated(HttpSessionEvent sessionEvent) {
		logger.error("HelpDesk system sessionCreated, sessionid=" + sessionEvent.getSession().getId());
	}

	public void sessionDestroyed(HttpSessionEvent sessionEvent) {
		String userJobNumber="";
		if(sessionEvent.getSession().getAttribute(ConstantDifinitionUtil.CURRENT_USER_ID)!=null){
			userJobNumber=(String)sessionEvent.getSession().getAttribute(ConstantDifinitionUtil.CURRENT_USER_ID);
		}
		logger.error("HelpDesk system sessionDestroyed, sessionid=" + sessionEvent.getSession().getId()
				+";登录用户工号是:"+userJobNumber);
	}
}

再写个类查看信息:


package org.hd.tempuser.action;

import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.hd.tempuser.service.TempUserService;
import org.rd.framework.common.container.ContainerManager;
import org.rd.framework.struts.action.CommonAction;

import com.opensymphony.xwork2.ActionContext;

public class AllUserMonitorAction extends CommonAction{
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(AllUserMonitorAction.class.getName());
	private TempUserService tempUserService = (TempUserService)ContainerManager.getComponent(TempUserService.BEAN_ID);
	
	//查看在线所有用户
	public String monitorAllUsersNow() throws Exception{
		
		ActionContext ctx = ActionContext.getContext();
		HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE); 
		HttpServletRequest request  = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST); 
		HttpSession session=request.getSession();
		Map appMap=ctx.getApplication();
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		
		System.err.println("session的id是:"+session.getId());
		session.setAttribute("haha", session.getId());
		
		return SUCCESS;
	}
	//查看当前的sessionId以及属性"haha"的值
    public String monitorUserInfoFromSession() throws Exception{
		
		ActionContext ctx = ActionContext.getContext();
		HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE); 
		HttpServletRequest request  = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST); 
		HttpSession session=request.getSession();
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		
		System.err.println("session的id是(session.getId()):"+session.getId());
		System.err.println("从session的属性中取出的id是(session.getAttribute(\"haha\")):"+session.getAttribute("haha"));
//		out.println("YES");
		return SUCCESS;
	}
	
	public String execute() throws Exception{
		return SUCCESS;
	}

}

 

开始测试:

第一个人访问登陆页面

<2012-12-11 14:46:00,977> ERROR (HDSessionListener.java:14) [http-80-1] (org.hd.base.listener.HDSessionListener) - HelpDesk system sessionCreated, sessionid=86938C672ADD0922845ED8DDD1575B6F

继续访问请求
http://helpdesk.citicsinfo.com/tempUser/monitorAllUsersNow.action

打印:
session的id是:86938C672ADD0922845ED8DDD1575B6F

访问请求
http://helpdesk.citicsinfo.com/tempUser/monitorUserInfoFromSession.action
打印:
session的id是(session.getId()):86938C672ADD0922845ED8DDD1575B6F
从session的属性中取出的id是(session.getAttribute("haha")):86938C672ADD0922845ED8DDD1575B6F


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值