项目需要做一个统计在线登陆用户数量的功能。
首先我想到的是写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