获得HttpServletRequest 和HttpSession对象

Posted 星朝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获得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);  
        }  
    }  

以上是关于获得HttpServletRequest 和HttpSession对象的主要内容,如果未能解决你的问题,请参考以下文章

文件上传 servlet 从HttpServletRequest.getInputStream()中获得消息内容

web服务器端获得HttpServletRequest对象

Java 在 HttpServletRequest 中获得 TRUE 起源

HttpServletRequest对象

SSH框架学习笔记----Filter,Listener以及JSP的新特性

通过HttpServletRequest转换获得json对象