WebUtils工具类获取Session

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

}

 

你可能感兴趣的:(#,Java基础,JavaWeb开发基础,web工具类)