쿠키(cookie)는 웹 브라우저가 보관하고 있는 데이터로서 웹 서버에 요청을 보낼 때 함께 전송
동작 방식
웹 브라우저에 쿠키가 저장되면, 웹 브라우저는 쿠키가 삭제되기 전까지 웹 서버에 쿠키를 전송함
웹 어플리케이션을 사용하는 동안 지속적으로 유지해야 하는 정보는 쿠키를 사용해서 저장하면 됨
구성요소
쿠키 이름의 제약
쿠키의 이름은 아스키 코드의 알파벳과 숫자만을 포함할 수 있다.
콤마(,), 세미콜론(;), 공백(' ') 등의 문자는 포함할 수 없다.
$로 시작할 수 없다.
보통 쿠키 이름을 작성할 때에는 알파벳과 숫자만 이용함
쿠키 값을 생성할 때에는 알맞은 방식으로 인코딩 함
별도 유효시간을 지정하지 않으면 웹 브라우저를 종료할 때 쿠키를 함께 삭제함. 지정한 도메인이나 경로로만 쿠키를 전송하도록 제한할 수도 있음
<%
Cookie cookie = new Cookie("cookieName", "cookieValue");
response.addCookie(cookie);
%>
클라이언트가 보낸 쿠키 읽기
Cookie[] cookies = request.getCookies();
읽기 관련 주요 메서드
메서드 리턴타입 설명 getName() String 쿠키의 이름을 구한다.
getValue() String 쿠키의 값을 구한다.
쿠키는 값으로 한글과 같은 문자를 가질 수 없음
-> 쿠키의 값을 인코딩해서 지정할 필요 있음
쿠키 값의 처리
-> 값 설정시 : URLEncoder.encode("값", "euc-kr")
예, new Cookie("name", URLEncoder.encode("값", "euc-kr"));
-> 값 조회시 : URLDecoder.decode("값", "euc-kr")
Cookie cookie = …;
String value = URLDecoder.decode(cookie.getValue(), "euc-kr");
Cookie[] cookies = request.getCookies();
if(cookies!=null && cookies.length > 0){
for(int i = 0; i<cookies.length; i++){
if(cookies[i].getName().equals("name")){
Cookie cookie = new Cookie(name, value);
response.addCookie(cookie);
}
}
}
[쿠키] 정리
1. 쿠키란?
쿠키 생성은?
Cookie cookie = new Cookie("memberid", "admin");
response.addCookie(cookie);
쿠키 삭제는?
cookie.setMaxAge(0);
response.addCookie(cookie);
쿠키 생성?
Cookie cookie = new Cookie(String name, String value);
쿠키 정보 받기
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>Cookie</title>
</head>
<body>
<form method="post" action="cookie01_process.jsp">
<p>아 이 디 : <input type="text" name="id" /></p>
<p>비밀번호 : <input type="text" name="passwd" /></p>
<p><input type="submit" value="전송" /></p>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>Cookie</title>
</head>
<body>
<%
//?id=admin&passwd=1234
String user_id = request.getParameter("id");
String user_pw = request.getParameter("passwd");
if(user_id.equals("admin")&&user_pw.equals("1234")){
//Cookie 객체 생성
Cookie cookie_id = new Cookie("userID", user_id);
Cookie cookie_pw = new Cookie("userPW", user_pw);
response.addCookie(cookie_id);
response.addCookie(cookie_pw);
out.print("쿠키 생성 성공!");
out.print(user_id + "님 환영합니다");
}else{
out.print("쿠키 생성 실패");
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
//쿠키 정보를 얻어오기
Cookie[] cookies = request.getCookies();
for(int i=0; i<cookies.length; i++){
out.print("설정된 쿠키의 속성 명 [" + i + "] : " + cookies[i].getName() + "<br>");
out.print("설정된 쿠키의 속성 값 [" + i + "] : " + cookies[i].getValue() + "<br>");
out.print("===============================<br>");
}
%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>Cookie</title>
</head>
<body>
<%
Cookie[] cookies = request.getCookies();
for(int i=0; i<cookies.length; i++){
//쿠키 삭제 : 유효기간을 0으로 설정
cookies[i].setMaxAge(0);
response.addCookie(cookies[i]);
}
response.sendRedirect("cookie02.jsp");
%>
</body>
</html>