본문 바로가기
스마트인재개발원/JSP . SERVLET

[JSP] 내가만든 쿠키가 생각나는 쿠키/쿠키굽기(생성)

by 죠졍니 2022. 10. 14.
728x90
반응형
SMALL

쿠키 vs 세션

- 사용 이유

HTTP(비연결형 프로토콜) 한계

 

 

- Cookie&Session란?

: Client의 정보를 지속적으로 유지하기 위한 방법

 

 

 

 

- Cookie란?

: 전달할 데이터를 Web Browser(Client)로 보냈다가 Web Server로 되돌려받는 방법

 

 

 

 

쿠키 확인을 잘 하기위한 추가 프로그램 설치

 

 

https://chrome.google.com/webstore/detail/editthiscookie/fngmhnnpilhplaeedifhccceomclgfbg/related?hl=ko

 

EditThisCookie

EditThisCookie는 쿠키 관리자입니다. 이것을 이용하여 쿠키를 추가하고, 삭제하고, 편집하고, 찾고, 보호하거나 막을 수 있습니다!

chrome.google.com

 

 

쿠키생성하기

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>

	<%
		//Cookie & Session - Client의 정보를 지속적으로 유지하기 위한 방법
		//Cookie - 저장할 데이터를 Client의 Browser에 저장하는 방법
		
		//Cookie 저장 흐름
		//1. 쿠키를 맛있게 굽는다(생성한다)
		//2. 쿠키안에 사랑의 메세지(전달 또는 저장하고싶은 내용)를 작성한다
		//3. Client에게 몰래 전달(전송)하면 성공
		Cookie cookie = new Cookie("id","pbk");
	
		//쿠키의 나이 설정
	    cookie.setMaxAge(60*60*24*365);
		response.addCookie(cookie);
		
		
		//Cookie의 특징
		//1. 쿠키는 Text형태의 자료만 저장할 수 있다
		//2. 쿠키의 나이를 따로 설정할 수있으니 나이를 설정안하면
		//   브라우저 종료시 코키는 만료된다
		//3. 쿠키는 무한정으로 생성이 불가능 하며 한 도메인 당 20개,
		//   쿠키 하나당 총 4kb까지 생성가능하다
		
		
	%>


</body>
</html>

 

 

쿠키 여러개 생성하기

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%
	//쿠키생성
	Cookie cookie1 = new Cookie("name1","조정은");
	Cookie cookie2 = new Cookie("name2","황예진");
	Cookie cookie3 = new Cookie("name3","잠와");
	
	//쿠키보내기
	response.addCookie(cookie1);
	response.addCookie(cookie2);
	response.addCookie(cookie3);
	
	
	//Client가 가지고 있는 Cookie 꺼내기(쿠키불러오기)
	Cookie[] cookies=request.getCookies();
	
	for(int i=0;i<cookies.length;i++){
		out.print("쿠키이름 : ");
		out.print(cookies[i].getName());
		out.print("&nbsp;쿠키값 : ");
		out.print(cookies[i].getValue());
		out.print("<br>");
	}
	
	%>
</body>
</html>

 

쿠키 지우기

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body><%
	//쿠키를 삭제하는 방법
	//- 동일한 이름을 가진 쿠키를 생성해서 Client에게 보내면
	//  최근에 생성한 쿠키로 덮어씌여짐
	//  그렇다면 삭제하고싶은 이름의 쿠키 생성해서
	//  나이를 0으로 하고 보내면 쿠기 사라짐
	Cookie cookie = new Cookie("id","");
	cookie.setMaxAge(0);
	response.addCookie(cookie);
%>

</body>
</html>

 

 

 

 

 

 

 

 

 

 

728x90
반응형
LIST