年底收藏系列-Java Web工具SessionTick

 

package org.lambdasoft.web.support; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import org.lambdasoft.web.Enviroment; public class SessionSupport { private SessionSupport() {} /** * 获取当前用户Session * * @return httpSession */ public final static HttpSession getSession() { return ServletActionContext.getRequest().getSession(); } @SuppressWarnings("unchecked") public final static void removeAllSession(HttpSession session) { if(session == null) return; Enumeration names = session.getAttributeNames(); if(names == null) return; List<String> sessionNamesList = new ArrayList<String>(); while (names.hasMoreElements()) sessionNamesList.add((String)names.nextElement()); for (String sessionName : sessionNamesList) { session.removeAttribute(sessionName); } } /** * 添加或者更新Session票据信息 * * @param sessionTick */ public final static void updateSessionTick(SessionTick<TickInterface> sessionTick) { getSession().removeAttribute(Enviroment.getEnv().getEnv("WEB_SESSION_KEY")); getSession().setAttribute(Enviroment.getEnv().getEnv("WEB_SESSION_KEY"), sessionTick); } /** * 把用户票据加入到Session * @param account */ public final static void addTickToSession(TickInterface account) { SessionTick<TickInterface> tick = new SessionTick<TickInterface>(); tick.setAccount(account); updateSessionTick(tick); } }

 

 

/* * SessionTick.java * Copyright (C) 2009 <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * */ package org.lambdasoft.web.support; /** * Session Support * * @author lei.tang ([email protected]) * @date * @version */ import java.io.Serializable; public class SessionTick<T extends TickInterface> implements Serializable{ private static final long serialVersionUID = 1L; private T account = null; public SessionTick() {} public SessionTick(T account) { this.account = account; } public T getAccount() { return account; } public void setAccount(T account) { this.account = account; } } package org.lambdasoft.web.support; public interface TickInterface { Long getId(); }

转自【http://www.blogjava.net/justinlei/archive/2009/12/18/306586.html】

 

你可能感兴趣的:(年底收藏系列-Java Web工具SessionTick)