Spring

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

hansory 2018. 6. 15.

HttpSession session = request.getSession();

HttpSession session = request.getSession(true);

HttpSession session = request.getSession(false);

getSession(true) -> 세션이 이미 있는지 확인을함, 이미 있다면 그 세션을 반환시키고 없으면 새로운 세션을 생성

getSession(false) -> 세션이 있다면 그 세션을 리턴하지만, 세션이 존재하지 않는다면 null을 리턴한다 .


getSession(true)에서는 기존의 세션이든 새로운 세션이든 무조건 세션을 받아옴

getSession(false)에서는 세션이 있는 경우에만 세션을 리턴하므로 반드시 null체크를 해야함


HttpSession session = request.getSession(); 는  HttpSession session = request.getSession(true); 과 동일




if(request.getSession(false) == null){


//do something...

}



boolean isAdmin(HttpServletRequest req) {

HttpSession session = req.getSession(false);

if ( session == null) {

return false;

}

String adminId = (String) session.getAttribute("log");

Integer grade = (Integer) session.getAttribute("grade");

if ( adminId != null && grade != null && grade >= 5 ) {

return true;

} else {

return false;

}

}


boolean hasLogin(HttpServletRequest req) {

HttpSession session = req.getSession(false);

if ( session == null) {

return false;

}

if ( session.getAttribute("userLoginInfo") == null ) {

return false;

}

return true;

}


'Spring' 카테고리의 다른 글

@SupperWarnings 란 ?  (0) 2018.06.27

댓글