

구성 요소
이름 : 각각의 쿠키를 구별하는 데 사용되는 이름
값 : 쿠키의 이름과 관련된 값
유효시간 : 쿠키의 유지 시간
도메인 : 쿠키를 전송할 도메인
경로 : 쿠키를 전송할 요청 경로
쿠키 이름의 제약
쿠키의 이름은 아스키 코드의 알파벳과 숫자만을 포함할 수 있다.
콤마(,), 세미콜론(;), 공백(' ') 등의 문자는 포함할 수 없다.
'$'로 시작할 수 없다.
예제
makeCookie.jsp
<%@page import="java.net.URLDecoder"%> <%@page import="java.net.URLEncoder"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% // 쿠키 객체를 생성 Cookie c1 = new Cookie("userID", "V"); // response.addCookie(쿠키객체) response.addCookie(c1); Cookie c2 = new Cookie("userName", URLEncoder.encode("김태형", "UTF-8")); response.addCookie(c2); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>쿠키생성, 저장</h1> <h3> <%= c1.getName()%>=<%=c1.getValue() %></h3> <h3> <%= c2.getName()%>=<%= URLDecoder.decode(c2.getValue(), "utf-8")%></h3> <a href="viewCookie.jsp">쿠키 정보 보기</a> </body> </html>
viewCookie.jsp
<%@page import="java.net.URLDecoder"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% // 쿠키 가져오기! Cookie[] cookies = request.getCookies(); if(cookies != null && cookies.length>0){ for(int i=0; i<cookies.length; i++){ String name = cookies[i].getName(); String value = URLDecoder.decode(cookies[i].getValue(),"UTF-8"); out.println("<h1>"+name+" : "+value+"</h1>"); } }else{ out.println("<h1>저장된 쿠키가 없습니다.</h1>"); } %> </body> </html>


쿠키 값 변경
쿠키값 그냥 똑같은 이름으로 해주면 덮어써짐!
예제
쿠키 수정하기 > editCookie.jsp 를 추가!
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% // V를 BTS로 변경 Cookie cookie = new Cookie("userID", "BTS"); response.addCookie(cookie); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>쿠키 userID의 값이 변경되었습니다.</h1> <a href="viewCookie.jsp">바뀐 쿠키 정보 확인 </a> </body> </html>



쿠키 삭제하기
쿠키의 유지시간을 0으로 설정해준다.
예제
쿠키 수정하기 > deleteCookie.jsp 를 추가!

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% // 쿠키 삭제를 위해 쿠키의 유지 시간을 0으로 설정해준다. Cookie cookie = new Cookie("userID",""); cookie.setMaxAge(0); response.addCookie(cookie); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>쿠키 userID의 값이 삭제되었습니다.</h1> <a href="viewCookie.jsp">삭제된 쿠키 정보 확인 </a> </body> </html>





예제
<%@page import="java.net.URLDecoder"%> <%@page import="java.net.URLEncoder"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% // 쿠키 객체를 생성 Cookie c1 = new Cookie("userID", "V"); // response.addCookie(쿠키객체) response.addCookie(c1); Cookie c2 = new Cookie("userName", URLEncoder.encode("김태형", "UTF-8")); c2.setMaxAge(60*20); // 20분 뒤 삭제! response.addCookie(c2); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>쿠키생성, 저장</h1> <h3> <%= c1.getName()%>=<%=c1.getValue() %></h3> <h3> <%= c2.getName()%>=<%= URLDecoder.decode(c2.getValue(), "utf-8")%></h3> <a href="viewCookie.jsp">쿠키 정보 보기</a> </body> </html>

'JAVA > Jsp&Servlet' 카테고리의 다른 글
[JSP] mysql JDBC (0) | 2020.12.18 |
---|---|
[JSP] session 기본 객체 (0) | 2020.12.18 |
[JSP] 쿠키처리를 위한 유틸리티 클래스 만들기 (0) | 2020.12.17 |
[JSP] 에러 (0) | 2020.12.17 |
[JSP] beans 빈즈 (0) | 2020.12.17 |
[JSP] 내장객체와 속성관리 / 생명주기 (0) | 2020.12.16 |
[JSP] 기본 객체와 영역 / 내장객체 / request / response / .. etc (0) | 2020.12.15 |