I try in Spring to get the HttpSession
like this. I know, there are better ways to do so, but this is a legacy code, so I have to handle it.
private static HttpSession getHttpSession() {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpSession httpSession = requestAttributes.getRequest().getSession();
return httpSession;
}
In the second line I get a NPE. The null
is returned from getRequest()
so getSession()
crashes.
The method works fine in general. But there is a case where it's not. I can't image the case where Spring can't get a session from a request.
Is there a special behavior I should know about this method?
Thanks! Robert.