JAVA를 JAVA라

[java] getSession(), getSession(true), getSession(false) 차이점 본문

JAVA/열일하는 블로그

[java] getSession(), getSession(true), getSession(false) 차이점

샛별KIM 2022. 1. 11. 13:55

 

getSession()과 getSession(true) 는 같은 의미로,

HttpSession이 존재하면 현재 세션을 반환하고 존재하지 않으면 새로 세션을 생성한다.

 

getSession(false)는 세션이 존재하면 현재 세션을 반환하고, 존재하지 않으면 null을 반환한다.

(새로 세션을 생성하지 않는 차이점이 있다.)

 

HttpSession session = request.getSession();
HttpSession session = request.getSession(true);
// 위 두개는 새 세션을 생성하므로 바로 getAtrribute() 가능

HttpSession session = request.getSession(false);
if (session != null){
    User user = (User) session.getAttribute("user_id");
}
// false는 세션이 없으면 null을 반환하므로 null체크가 반드시 필요하다.

출처 : https://july1012.tistory.com/entry/getSession-getSessiontrue-getSessionfalse-%EC%B0%A8%EC%9D%B4%EC%A0%90

 

출처)

 

getSession(), getSession(true), getSession(false) 차이점

1. getSession(), getSession(true)  - HttpSession이 존재하면 현재 HttpSession을 반환하고 존재하지 않으면 새로이 세션을 생성합니다 2. getSession(false)  - HttpSession이 존재하면 현재 HttpSession을..

july1012.tistory.com

 

Comments