欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WebUtils工具类获取Session

程序员文章站 2024-03-20 20:00:10
...
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
* @author leon
* @createDate 2018年5月9日 下午2:58:09
* @version 1.0
* @classRemarks web工具类
*/
public class WebUtils {
	
	public static HttpSession getSession() {
        HttpSession session = null;
        try {
            session = getRequest().getSession();
        } catch (Exception e) {}
        return session;
    }

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs!=null?attrs.getRequest():null;
    }

    public static HttpServletResponse getResponse(){
        ServletRequestAttributes attrs = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes());
        return attrs!=null?attrs.getResponse():null;
    }

}

 

相关标签: web工具类