민서네집

[java] request.getSession(); 값이 null 이 될 수 있다. 본문

Java

[java] request.getSession(); 값이 null 이 될 수 있다.

브라이언7 2013. 7. 29. 09:32

HttpSession session = request.getSession();


위 코드에서 getSession() 의 설명을 찾아보면,


HttpSession getSession()

Returns the current session associated with this request, or if the request does not have a session, creates one.

Returns:
the HttpSession associated with this request
See Also:
getSession(boolean)

이렇게 나와 있다.

그래서 null 이 안 나올 줄 알았는데, null 이 나오는 경우가 있었다.

request 변수를 static 변수로 만들었는데, (물론 이렇게 만들면 안된다.)

A request 에서 B request 값을 통해 getSession() 을 호출했더니, getSession() 이 null 이 되었다.

이 경우 request.isRequestedSessionIdValid() 를 호출해 보았더니, false 가 나왔다.


boolean isRequestedSessionIdValid()

Checks whether the requested session ID is still valid.

If the client did not specify any session ID, this method returns false.

Returns:
true if this request has an id for a valid session in the current session context; false otherwise
See Also:
getRequestedSessionId(), getSession(boolean), HttpSessionContext

이런 특별한 경우가 아니더라도, session 값이 있기는 있되, 만료되서 invalidate 된 session 이라면

request.getSession() 의 값이 null 이 나올것 같다.


Comments