获得HttpServletRequest 和HttpSession对象
程序员文章站
2024-02-04 21:07:40
...
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);
}
}
}
上一篇: python打包成exe文件
下一篇: 小白爬虫第一弹之抓取妹子图
推荐阅读
-
获得HttpServletRequest 和HttpSession对象
-
【java】通过HttpServletRequest转换获得json对象
-
「iOS」swift 和 objectivec 获得对象的 class 或者 Type 的方法
-
直接获取HttpServletRequest和HttpSession对象
-
JS获得一个对象的所有属性和方法实例
-
JS获得一个对象的所有属性和方法实例
-
BootStrap 获得轮播中的索引和当前活动的焦点对象
-
wicket获得原始的HttpServletRequest 和HttpServletResponse
-
jQuery获得document和window对象宽度和高度的方法教程
-
BootStrap 获得轮播中的索引和当前活动的焦点对象