数据表:
web.xml中添加
监听对象:
获取Spring beans的工厂对象
用户名在用户登录时候设置,用户登出是清除Session,方法为session.invalidate();
此方法无法记录用户在最后一个页面上的停留时间,无法获取浏览器关闭事件的信息,不过如果只是用于记录注册用户的在线时间还是可以的。
create table SESSION_LOG ( ID NUMBER not null, SESSION_ID VARCHAR2(100), CREATE_DATE NUMBER, LAST_ACCESSED_DATE NUMBER, LOGIN_ID VARCHAR2(40), IP_ADDRESS VARCHAR2(32) )
web.xml中添加
<listener> <listener-class> org.zju.cdpsn.module.listener.CDPSNSessionListener </listener-class> </listener>
监听对象:
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; import org.zju.cdpsn.biz.dao.sessionlog.SessionLogDAO; import org.zju.cdpsn.biz.dao.sessionlog.dataobject.SessionLog; import org.zju.cdpsn.module.fm.screen.factory.ScreenFactory; import org.zju.cdpsn.module.util.ClientUtil; public class CDPSNSessionListener implements HttpSessionListener,ServletRequestListener { private HttpServletRequest request; @Override public void sessionCreated(HttpSessionEvent se) { // 获取将要删除的session HttpSession session = se.getSession(); // 获取会话表DAO SessionLogDAO sessionLogDAO = (SessionLogDAO) GetBeanFactory.getInstance().getBean("sessionLogDAO"); // 初始化记录对象 SessionLog record = new SessionLog(); // 获取ClientIP String clientIP = ClientUtil.getClientIp(request); record.setIpAddress(clientIP); // 获取sessionId String sessionId = session.getId(); record.setSessionId(sessionId); // 获取创建时间 long crtimes = session.getCreationTime(); record.setCreateDate(crtimes); // 更新记录 sessionLogDAO.insert(record); } @Override public void sessionDestroyed(HttpSessionEvent se) { // 获取将要删除的session HttpSession session = se.getSession(); // 获取会话表DAO SessionLogDAO sessionLogDAO = (SessionLogDAO) GetBeanFactory.getInstance().getBean("sessionLogDAO"); // 初始化记录对象 SessionLog record = new SessionLog(); // 获取sessionId String sessionId = session.getId(); record.setSessionId(sessionId); // 获取最后访问时间 long latimes = session.getLastAccessedTime(); record.setLastAccessedDate(latimes); // 更新记录 sessionLogDAO.updateBySessionIdSelective(record); } @Override public void requestInitialized(ServletRequestEvent sre) { this.request = (HttpServletRequest) sre.getServletRequest(); } @Override public void requestDestroyed(ServletRequestEvent sre) { } public void setRequest(HttpServletRequest request) { this.request = request; } public HttpServletRequest getRequest() { return request; } }
获取Spring beans的工厂对象
import org.apache.commons.lang.StringUtils; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * Spring beans工厂类 * * @author xie * @date 2010-05-28 */ public class GetBeanFactory implements ApplicationContextAware { private boolean alreadyInit = false; private ApplicationContext appContext; private GetBeanFactory() { } private static class SingletonHolder { private static GetBeanFactory factory = new GetBeanFactory(); } /** * 获得工厂静态实例 */ public static GetBeanFactory getInstance() { return SingletonHolder.factory; } /** * 根据名称取得Spring Beans对象 * * @param beanName * @return Spring Beans */ @SuppressWarnings("unchecked") public <T> T getScreenBean(String beanName) { if (StringUtils.isBlank(beanName)) { return null; } return (T) appContext.getBean(beanName); } /** * 设置ApplicationContext */ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { GetBeanFactory getBeanFactory = GetBeanFactory.SingletonHolder.factory; if (!getBeanFactory.alreadyInit) { getBeanFactory.appContext = applicationContext; getBeanFactory.alreadyInit = true; } } }
用户名在用户登录时候设置,用户登出是清除Session,方法为session.invalidate();
此方法无法记录用户在最后一个页面上的停留时间,无法获取浏览器关闭事件的信息,不过如果只是用于记录注册用户的在线时间还是可以的。