[Servlet_JSP] Cookie&Session
2024. 3. 31. 21:40ㆍBE/Servlet_JSP
[Node.js] 쿠키 생성, 읽기
0. 참고자료 Node.js - 쿠키와 인증 - 생활코딩 수업소개 이 수업은 웹서버의 정보를 웹브라우저에 저장해서 개인화, 인증, 사용자 추적 등의 기능을 구현할 수 있도록 해주는 쿠키(cookie)를 알려드
ramen4598.tistory.com
[Express] Session이란?
1. 세션 앞서 쿠키를 사용해서 인증을 구현하면 생기는 문제점에 대하여 알아보았다. [Node.js] 쿠키의 한계 1. 인증 부분의 한계 민감한 정보를 클라이언트 쪽에 저장한다는 것은 위험하다. 또한
ramen4598.tistory.com
1. Cookie
Method | Description |
Cookie[] cookies = request.getCookies(); | 요청에서 쿠키를 가져옴. |
response.addCookie(new Cookie("name", "value")); | 응답에 쿠키를 추가. |
String name = cookie.getName(); | 이름을 반환. |
cookie.setValue("newValue"); | 값을 설정. |
String value = cookie.getValue(); | 값을 반환. |
cookie.setMaxAge(60); // 60 seconds | 유지되는 시간을 설정. |
int maxAge = cookie.getMaxAge(); | 유지되는 시간을 반환. |
cookie.setDomain("example.com"); | 송신하는 도메인을 설정. |
String domain = cookie.getDomain(); | 속하는 도메인을 반환. |
String path = cookie.getPath(); | 속하는 경로를 반환합. |
cookie.setPath("/path"); | 송신하는 경로를 설정. |
String comment = cookie.getComment(); | 주석을 반환. |
cookie.setComment("User session cookie"); | 주석을 지정. |
boolean secure = cookie.getSecure(); | 사용자의 브라우저와 웹 서버 사이에서 안전하게 전송되는지 반환. |
cookie.setSecure(true); | 사용자의 브라우저와 웹 서버 사이에서 안전하게 전송되도록 설정. |
int version = cookie.getVersion(); | 버전을 반환. |
cookie.setVersion(1); | 버전을 설정. |
- 쿠키를 가져오는 방법
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for(Cookie cookie : cookies) {
if("name".equals(cookie.getName())) {
value = cookie.getValue();
break;
}
}
}
2. HttpSession Session
Method | Description |
HttpSession session = reqeust.getSession(); | 요청에서 세션 정보를 확인한다. |
String id = session.getId(); | 현재 세션의 고유 식별자를 반환합니다. |
session.setAttribute("name", value); | 이 세션에 객체를 바인딩합니다. |
Object obj = session.getAttribute("name"); | 이 세션에 바인딩된 객체를 반환합니다. |
session.removeAttribute("name"); | 이 세션에서 객체의 바인딩을 제거합니다. |
session.invalidate(); | 이 세션을 무효화하고 세션을 바인딩하는 모든 객체를 언바인드합니다. |
long ct = session.getCreationTime(); | 이 세션이 생성된 시간을 반환합니다. |
long lat = session.getLastAccessedTime(); | 클라이언트가 이 세션에 마지막으로 요청한 시간을 반환합니다. |
session.setMaxInactiveInterval(interval); | 클라이언트 요청 사이의 최대 시간 간격을 설정합니다. |
int interval = session.getMaxInactiveInterval(); | 클라이언트 요청 사이의 최대 시간 간격을 반환합니다. |
ServletContext context = session.getServletContext(); | 이 세션을 생성한 ServletContext를 반환합니다. |
boolean isNew = session.isNew(); | 클라이언트가 아직 세션을 인식하지 못했는지 확인합니다. |
가. 생성
HttpSession session = reqeust.getSession();
HttpSession session = reqeust.getSession(false);
getSession()
,getSession(true)
:HttpSession
이 존재하면 현재HttpSession
을 반환
: 존재하지 않으면 새로운 세션을 생성getSession(false)
:HttpSession
이 존재하면 현재HttpSession
을 반환
: 존재하지 않으면null
을 반환
'BE > Servlet_JSP' 카테고리의 다른 글
[Servlet_JSP] JSTL (0) | 2024.04.08 |
---|---|
[Servlet_JSP] EL (0) | 2024.04.08 |
[Servlet_JSP] JSP (0) | 2024.04.01 |
[Servlet_JSP] Servlet (0) | 2024.03.31 |