码迷,mamicode.com
首页 > Web开发 > 详细

获得HttpServletRequest 和HttpSession对象

时间:2018-05-24 11:53:59      阅读:553      评论:0      收藏:0      [点我收藏+]

标签:http   hash   mvc   package   ext   ESS   parameter   param   move   

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);  
        }  
    }  

获得HttpServletRequest 和HttpSession对象

标签:http   hash   mvc   package   ext   ESS   parameter   param   move   

原文地址:https://www.cnblogs.com/jpfss/p/9081531.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!