상세 컨텐츠

본문 제목

쿠키(Cookie)

WebProgramming/JSP

by ChrisMare 2018. 10. 18. 21:17

본문

쿠키(Cookie) ?

웹브라우저에서 서버로 어떤 데이터를 요청 하면, 서버측에서는 알맞은 로직을 수행한 후 데이터를 웹브라우저에 응답한다.

그리고, 서버는 웹브라우저와의 관계를 종료한다.

이 처럼 웹브라우저에 응답 후 관계를 끊는 것은 http프로토콜의 특징이다.

연결이 끊겼을 때 어떤 정보를 지속적으로 유지하기 위한 수단으로 쿠키라는 방식을 사용한다.


쿠키서버에서 생성되며, 서버가 아닌 클라이언트측에 특정 정보를 저장한다.

그리고, 서버에 요청 할 때 마다 쿠키의 속성값을 참조 또는 변경 할 수 있다.

또한, 쿠키는 4kb로 용량이 제한적이며, 300개까지 데이터 정보를 저장할 수 있다.


쿠키 문법

과정)


쿠키 관련 메소드

 메소드 명

 기능

 setMaxAge( )

 쿠키 유효기간을 설정 한다. ( 쿠기의 유지시간을 설정 )

 setPath( )

 쿠키사용의 유효 디렉토리를 설정

 setValue( )

 쿠키의 값을 설정 

 setVersion( )

 쿠키 버전을 설정 

 getMaxAge( )

 쿠키 유효기간 정보를 가져온다. 

 getName( )

 쿠키 이름을 가져온다. 

 getPath( )

 쿠키사용의 유효 디렉토리 정보를 가져온다. 

 getValue( )

 쿠키의 값을 가져온다. 

 getVersion( )

 쿠키 버전을 가져온다. 


사용법)

주의 사항 : 속성이 변경된다면 다시 response.addCookie(cookie)로 담아야된다.

cookieServer.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>CookieServer</title>
</head>
<body>
<%
    Cookie cookie = new Cookie("cookieName","cookieValue");
    cookie.setMaxAge(60);    // 1분
    response.addCookie(cookie);
%>
 
<a href="cookieGet.jsp">cookieGet</a>
</body>
</html>
cs


cookieGet.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>cookieGet</title>
</head>
<body>
<%
    Cookie[] cookies = request.getCookies();
    
    for(int i=0; i<cookies.length; i++){
        String str = cookies[i].getName();
        if(str.equals("cookieName")) {
            out.println("cookies[" + i + "] name : " + cookies[i].getName() + "</br>");
            out.println("cookies[" + i + "] value : " + cookies[i].getValue() + "</br>");
            out.println("<hr>");
        }
    }
%>
 
<a href="cookieDelete.jsp">cookie Delete</a>
</body>
</html>
cs


cookieDelete.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>cookieDelete</title>
</head>
<body>
<%
    Cookie[] cookies = request.getCookies();
    for(int i=0;i<cookies.length; i++){
        String str = cookies[i].getName();
        if(str.equals("cookieName")) {
            out.println("name : " + cookies[i].getName() + "</br>");
            cookies[i].setMaxAge(0);
            response.addCookie(cookies[i]);
        }
    }
%>
 
<a href="cookieCheck.jsp">쿠키 확인</a>
</body>
</html>
cs


cookieCheck.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>cookieCheck</title>
</head>
<body>
<%
    Cookie[] cookies = request.getCookies();
    if(cookies != null){
        for(int i=0;i<cookies.length; i++){
            out.println(cookies[i].getName() + "</br>");
            out.println(cookies[i].getValue() + "</br>");
        }
    }
%>
</body>
</html>
cs



'WebProgramming > JSP' 카테고리의 다른 글

예외 페이지  (0) 2018.10.19
세션(session)  (0) 2018.10.19
request/response 객체  (0) 2018.10.18
JSP 동작 원리  (0) 2018.10.18
JSP 태그 개념 및 종류  (0) 2018.10.17

관련글 더보기

댓글 영역