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