web系统在线用户管理实现

1、创建一个sessionBean类,用来封装session里面自己需要的参数,将其封装成对象;

package com.lh.bean;

import java.io.Serializable;
import java.util.Calendar;

import javax.servlet.http.HttpSession;

import com.mbpsoft.shop.common.BussinessUtil;
import com.mbpsoft.shop.entity.Admin;

public class SessionBean implements Serializable {

	private static final long serialVersionUID = -7519486823153844426L;
	private String sessionid; // 会话编号
	private String creationTime;// 创建时间
	private Admin admin;//登录人
	private HttpSession session;
	private Calendar cal = Calendar.getInstance();
	public SessionBean(HttpSession session,Admin admin ){
		this.sessionid = session.getId();
		cal.setTimeInMillis(session.getCreationTime());
		this.creationTime = BussinessUtil.time2String(cal.getTime());
		this.session = session;
		this.admin = admin;
	}
	public String getSessionid() {
		return sessionid;
	}
	public void setSessionid(String sessionid) {
		this.sessionid = sessionid;
	}
	public String getCreationTime() {
		return creationTime;
	}
	public void setCreationTime(String creationTime) {
		this.creationTime = creationTime;
	}
	//最后运行时间
	public String getLastAccessedTime() {
		cal.setTimeInMillis(this.session.getLastAccessedTime());
		return  BussinessUtil.time2String(cal.getTime());
	}
	//可以得到用户实时的在线时长
	@SuppressWarnings("finally")
	public String getAccessedTime() {
		String time = "";
		try {
			time = (Calendar.getInstance().getTimeInMillis()-this.session.getCreationTime())/1000/60+"";
		} catch (Exception e) {
			//BussinessUtil.sessionBeans.remove(this.admin.getId());
			//BussinessUtil.sessions.remove(this.admin.getId());
			this.session = null;
			time = "∞";
		} finally {
			return time; 
		}
	}
	@SuppressWarnings("finally")
	public String getWaitingTime() {
		String time = "";
		try {
			time = (Calendar.getInstance().getTimeInMillis()-this.session.getLastAccessedTime())/1000/60+"";
		} catch (Exception e) {
			//BussinessUtil.sessionBeans.remove(this.admin.getId());
			//BussinessUtil.sessions.remove(this.admin.getId());
			this.session = null;
			time = "∞";
		} finally {
			return time; 
		}
	}
	public HttpSession getSession() {
		return session;
	}
	public void setSession(HttpSession session) {
		this.session = session;
	}
	@SuppressWarnings("finally")
	public int getIsValitate(){
		long time = 0l;
		int flag = 0;
		try {
			if(this.session == null){
				flag = 2;
			}else{
				time = (Calendar.getInstance().getTimeInMillis()-this.session.getLastAccessedTime())/1000/60;
				if(time>20)
					flag = 1;
				else
					flag = 0;
			}
		} catch (Exception e) {
			flag = 2;
		} finally {
			return flag;
		}
	}
}

 
  
 
  
 
  

 
  

2、定义一个全局静态变量sessionBeans(key为当前用户编号,value为用户登录系统建立的会话),在用户登录成功进行权限加载时,将该用户会话存入sessionBeans;

public static Map sessionBeans = new HashMap();
3、将sessionBeans放入session里,方便在前台页面进行用户管理;
getServletContext().setAttribute("sessionBeans", sessionBeans);

4、通过spring来进行定时执行任务,进行会话管理

package com.lh.quartz;

import java.util.Iterator;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mbpsoft.shop.bean.SessionBean;
import com.mbpsoft.shop.common.BussinessUtil;
import com.mbpsoft.shop.service.AdminService;

public class QuartzJob {

	
	@Resource(name="synchronizeDataServiceImpl")
	private SynchronizeDataService synchronizeDataService;
	@Resource(name = "adminServiceImpl")
	private AdminService adminService;
	
	public Log log = LogFactory.getLog(this.getClass());
	
	public void checkSession() {
		Map map = BussinessUtil.sessionBeans;
		Iterator keyIterator = map.keySet().iterator();
		try {
			while(keyIterator.hasNext()){
				String key = keyIterator.next();
				SessionBean sessionBean = map.get(key);
				int flag = sessionBean.getIsValitate();
				if(flag>0){
					adminService.deleteRightOnTime(sessionBean.getSessionid());
					BussinessUtil.sessionBeans.remove(key);
					BussinessUtil.sessions.remove(key);
					if(flag>1){
						log.info("注销用户:"+key+"异常用户");
					}else{
						log.info("注销用户:"+key+"超时用户");
						sessionBean.getSession().invalidate();
					}
				}
			}
		} catch (Exception e) {
			log.info("用户已异常注销");
			checkSession();
		}
	}
}






	
	
	



	
	
	








 




5、最终展示页面如下,方便随时管理在线用户

 
   
  

你可能感兴趣的:(开发日志)