zoukankan      html  css  js  c++  java
  • 全局Session-GlobalSession

    import javax.servlet.http.HttpServletRequest;  
    import javax.servlet.http.HttpSession;  
      
    import org.springframework.web.context.request.RequestContextHolder;  
    import org.springframework.web.context.request.ServletRequestAttributes;  
      
    /** 
     * 当前session工具类 
     * 
     */  
    public class GlobalSession {  
      
        public static void setSessionAttribute(String arg, Object o) {  
            HttpSession session = getSession();  
            session.setAttribute(arg, o);  
        }  
          
        public static Object getSessionAttribute(String arg) {  
            HttpSession session = getSession();  
            return session.getAttribute(arg);  
        }  
      
        public static void removeSessionAttribute(String arg) {  
            HttpSession session = getSession();  
            if (getSessionAttribute(arg) != null) {  
                session.removeAttribute(arg);  
            }  
        }  
      
        public static void setAttribute(String arg, Object o) {  
            HttpSession session = getHttpSession();  
            session.setAttribute(arg, o);  
        }  
      
      
        public static Object getAttribute(String arg) {  
            HttpSession session = getHttpSession();  
            return session.getAttribute(arg);  
        }  
      
      
        public static void removeAttribute(String arg) {  
            if (getAttribute(arg) != null) {  
                getHttpSession().removeAttribute(arg);  
            }  
        }  
      
          
        public static HttpSession getHttpSession() {  
            HttpServletRequest session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();  
            return session.getSession();  
        }  
      
          
        public static HttpSession getSession() {  
            HttpServletRequest session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();  
            return session.getSession();  
        }  
    }  
    
    

    使用方法:

    
    HttpServletRequest   request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    
  • 相关阅读:
    数据库常用操作命令以及explain执行计划
    spring中父子容器
    为什么SpringCloud引导类不加@EnableDiscoveryClient也可以注册到eureka中
    使用dubbo的注解,AOP配置xml的方式无法开启事务
    Excel导出打印失败报错 (eg HSSF instead of XSSF)
    0317 ajax
    0316 事务
    0316 DBUtils
    0315 el技术和jstl技术 javaEE开发模式
    0313 jsp
  • 原文地址:https://www.cnblogs.com/jpfss/p/9072212.html
Copyright © 2011-2022 走看看