• 获得HttpServletRequest 和HttpSession对象


    package org.jeecgframework.core.util;  
      
    import java.util.HashMap;  
    import java.util.Map;  
    import javax.servlet.http.HttpServletRequest;  
    import javax.servlet.http.HttpSession;  
    import org.springframework.web.context.request.RequestContextHolder;  
    import org.springframework.web.context.request.ServletRequestAttributes;  
      
    /** 
    * @ClassName: ContextHolderUtils  
    * @Description: TODO(上下文工具类)  
    * @author  张代浩  
    * @date 2012-12-15 下午11:27:39  
    * 
     */  
    public class ContextHolderUtils {  
        /** 
         * SpringMvc下获取request 
         *  
         * @return 
         */  
        public static HttpServletRequest getRequest() {  
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();  
            return request;  
      
        }  
      
        /** 
         * SpringMvc下获取session 
         *  
         * @return 
         */  
        public static HttpSession getSession() {  
            HttpServletRequest request = getRequest();  
            String tempSessionId = request.getParameter("sessionId");  
            HttpSession session = request.getSession();  
            String sessionId = session.getId();  
            if(StringUtil.isNotEmpty(tempSessionId) && !tempSessionId.equals(sessionId)){  
                sessionId = tempSessionId;  
                if(sessionMap.containsKey(sessionId)){  
                    session = sessionMap.get(sessionId);  
                }  
            }  
            if(!sessionMap.containsKey(sessionId)){  
                sessionMap.put(sessionId, session);  
            }  
            return session;  
        }  
      
          
        private static final Map<String, HttpSession> sessionMap = new HashMap<String, HttpSession>();  
          
        public static HttpSession getSession(String sessionId){  
            HttpSession session = sessionMap.get(sessionId);  
            return session == null ? getSession() : session;  
        }  
          
        public static void removeSession(String sessionId){  
            if(sessionMap.containsKey(sessionId)){  
                sessionMap.remove(sessionId);  
            }  
        }  
    
  • 相关阅读:
    mysql的存储过程与事务入门
    关于CreateProcess函数一些经验
    windows下dump文件调试
    指针相关
    nsi脚本中执行.bat文件要隐藏dos窗口问题
    C 预处理
    __declspec(dllimport)与__declspec(dllexport)作用总结
    [转]window下使用SetUnhandledExceptionFilter捕获让程序的崩溃
    字符集与编码[转]
    vc下项目的头文件包含目录以及库导入预计库目录设置
  • 原文地址:https://www.cnblogs.com/jpfss/p/9081531.html
Copyright © 2020-2023  润新知