쿠키란 사용자가 웹사이트를 처음 방문할때 server에 생성되

client의 pc에 저장되는 4KB의 작은 파일이다

 

cookie 생성은 cookie class를 사용하고 setter를 이용해 쿠키 속성을 설정한다

addCookie()를 이용해 response객체에 저장후 쿠키를 전송한다

 

쿠키 관련 method

setMaxAge(): 쿠키 유효기간을 설정

setPath(): 쿠키사용을 위한 directory를 설정(특정 경로명을 갖는 URL로 전송하도록 설정)

setValue(): 쿠키값을 설정

setVersion(): 쿠키의 버전을 설정

getMaxAge(): 쿠키의 유효기간 정보를 얻어온다

getName(): 쿠키의 이름을 얻어온다

getPath(): 쿠키의 유효 디렉토리 정보를 얻어온다

getVersion(): 큐키의 버전을 얻어온다

getCookies(): 쿠키 데이터를 읽어올때 사용한다

 

저장된 쿠키 사용 순서

1. 웹브라우저의 요청에서 쿠키를 얻어온다

2. 쿠키는 이름, 값의 쌍으로된 배열형태로 return 된다

3. return된 쿠키의 배열에서 쿠키의 이름을 가져온다

4. 쿠키의 이름을 통해서 해당 쿠키의 설정된 값을 추출한다

---쿠키생성

<%

String cookieName = "id";

Cookie cookie = new Cookie(cookieName, "test");

cookie.setMaxAge(60*30);

response.addCookie(cookie);

%>

---생성된 쿠키확인

<%

Cookie[] cookies = request.getCookies();

if(cookies != null){

for(int i=0; i<cookies.length; i++){

String str = cookies[i].getName();

if(str.equals("id")){

out.println(i+cookies[i].getName());

out.println(i+cookies[i].betValue());

}}}

%>

---쿠키삭제

<%

Cookie[] cookies = request.getCookies();

for(int i=0, i<cookies.length; i++){

String str = cookies[i].getName();

if(str.equals("id")){

out.println(cookies[i].getName());

cookies[i].setMaxAge(0);

response.addCookie(cookies[i]);

}}

%>

---삭제된 쿠키확인

<%

Cookie[] cookies = request.getCookies();

if(cookies != null){

for(int i=0; i<cookies.length; i++){

out.println(cookies[i].getName());

out.println(cookies[i].getName());

}}

%>

'JSP' 카테고리의 다른 글

예외처리(error page)  (0) 2019.08.14
session  (0) 2019.08.14
request, response, action tag  (0) 2019.08.14
JSP tag 종류, page, include, taglib 지시자, jsp 내부객체  (0) 2019.08.14
register and database list  (0) 2019.08.09

+ Recent posts