- 웹서버 (Http) : stateless(상태X)
- req+res ➔ 연결 끊김
- 1(req+res) --정보 유지 X-- 2(req+res)
⇨ 웹서버 자체는 상태 관리X
- 웹 어플리케이션 서버(WAS) : 상태 관리 필요
✅ 상태 정보를 서버에서 관리하는 메커니즘
✅ 서버와 클라이언트 간 지속적 연결 상태를 가짐
✅ HttpSession 클래스
ex) 로그인, 장바구니 ...
request.getSession()sess.setAttribute(key, value) : 속성 값 설정sess.getAttribute(key) : 속성 값 반환sess.removeAttribute(key) : 속성 제거setMaxInactiveInterval(sec) : 지정된 시간만큼 세션 유지 (초sec)getMaxInactiveInterval(sec) : 세션 유지시간 반환sess.invalidate()@WebServlet("/cart_view")
public class CartViewServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("<html><body>");
out.print("장바구니 리스트");
//세션 객체 얻기
//false: 세션을 생성하지 않고, 이미 존재하는 세션만 반환하도록 요청
HttpSession session = request.getSession(false);
if(session != null) {
//if문 밖에 있으면 비어있는 세션
// -> null.setMaxInactiveInterval()이 되어서 에러 - 밑에 코드 실행 안됨~!
session.setMaxInactiveInterval(10); //10초
ArrayList<String> list = (ArrayList<String>) session.getAttribute("product");
out.print(" 상품: " + list + "<br>");
} else {
out.print(" 세션 없음" + "<br>");
}
out.print("<a href='session_product.jsp'>상품 선택 페이지</a><br>");
out.print("<a href='cart_delete'>장바구니 비우기</a><br>");
out.print("</body></html>");
}
}
✅ 상태 정보를 클라이언트에서 관리하는 메커니즘
✅ 클라이언트에서 관리해서 서버 부하↓ & 보안 취약
✅ 브라우저 메모리 or OS 파일에 저장 가능 (default: 브라우저 메모리)
✅ 매번 요청을 보낼 때마다 자동으로 헤더에 담겨서 전송 됨
✅ 발급: 서버 / 관리: 클라이언트
Cookie(name, value) : 쿠키 생성response.addCookie(cookie) : 생성된 쿠키를 응답 처리 ➔ 헤더의 Set-Cookie에 담김request.getCookies() : 클라이언트로부터 쿠키 정보 배열로 리턴✅ 서버가 직접 삭제 X
✅ MaxAge를 아주 짧은 시간 줌
setMaxAge(sec) : 쿠키 만료 시간 설정
- 쿠키는 있으면 무조건 다 요청 헤더에 담아서 보냄, 운영 성격 안 따짐
장바구니에서 나온 쿠키인데 루트 응답 헤더에 있움냐