[Servlet 4-1] Cookie

์ž„์Šนํ˜„ยท2022๋…„ 12์›” 1์ผ
0

Servlet

๋ชฉ๋ก ๋ณด๊ธฐ
9/14
post-thumbnail

๐Ÿง์ฟ ํ‚ค๋ฅผ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌํ•˜๋Š” ์„œ๋ธ”๋ฆฟ

๐Ÿ“Œ์ฟ ํ‚ค(Cookie) : ์„œ๋ฒ„(์›นํ”„๋กœ๊ทธ๋žจ)์™€ ํด๋ผ์ด์–ธํŠธ(๋ธŒ๋ผ์šฐ์ €)์˜ ์—ฐ๊ฒฐ ์ง€์†์„ฑ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ๋˜๋Š” ๊ฐ’
โ†’ ์ฟ ํ‚ค๋Š” ์ ‘์† ์„œ๋ฒ„ ์ •๋ณด๋ฅผ ์‹๋ณ„์ž๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ

๐Ÿ“ข 1.Cookie ํด๋ž˜์Šค๋กœ ๊ฐ์ฒด ์ƒ์„ฑ
โ†’ Cookie ๊ฐ์ฒด : ์ฟ ํ‚ค ๊ด€๋ จ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด
๐Ÿ“Œ Cookie(String name, String Value) : ์ฟ ํ‚ค๋ช…๊ณผ ์ฟ ํ‚ค๊ฐ’์„ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ „๋‹ฌํ•˜์—ฌ ๊ฐ์ฒด ์ƒ์„ฑ
โ†’ ์ฟ ํ‚ค๋ช… : ์ฟ ํ‚ค๊ฐ’์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ ์‹๋ณ„์ž
โ†’ ์ฟ ํ‚ค๊ฐ’ : ์—ฐ๊ฒฐ ์ง€์†์„ฑ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ๋ฌธ์ž๊ฐ’
โ†’ ์ฟ ํ‚ค๋ช…๊ณผ ์ฟ ํ‚ค๊ฐ’์€ ์˜๋ฌธ์ž,์ˆซ์ž,์ผ๋ถ€ ํŠน์ˆ˜๋ฌธ์ž๋งŒ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑ ๊ฐ€๋Šฅ

Cookie idCookie=new Cookie("id", "abc123");
Cookie countCookie=new Cookie("count", "0");

โ€ป ์ž๋ฐ”์—์„œ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค์‚ฌ์šฉ(์ฒซ๋ฒˆ์งธ๊ฑฐ)

๐Ÿ“ข 2.ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌ๋˜์–ด ์ €์žฅ๋  ์ฟ ํ‚ค์˜ ์œ ์ง€์‹œ๊ฐ„ ๋ณ€๊ฒฝ
๐Ÿ“Œ Cookie.setMaxAge(int expiry) : ์ฟ ํ‚ค์˜ ์œ ์ง€์‹œ๊ฐ„(์ดˆ)์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฉ”์†Œ๋“œ
๐Ÿ“Œ ์ฟ ํ‚ค์˜ ์œ ์ง€์‹œ๊ฐ„์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’์€ [-1๋กœ] ์ž๋™ ์„ค์ •
โ†’ ์ฟ ํ‚ค์˜ ์œ ์ง€์‹œ๊ฐ„์ด [-1]๋กœ ์„ค์ •๋œ ๊ฒฝ์šฐ ์ฟ ํ‚ค๋Š” ๋ธŒ๋ผ์šฐ์ € ์ข…๋ฃŒ์‹œ ์ž๋™ ์†Œ๋ฉธ

countCookie.setMaxAge(24*60*60);

โ€ป ์ฟ ํ‚ค ์œ ์ง€์‹œ๊ฐ„ : 1์ผ

๐Ÿ“ข 3.ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ฟ ํ‚ค๋ฅผ ์ „๋‹ฌ - ์ฟ ํ‚ค๋ฅผ ์ „๋‹ฌ ๋ฐ›์€ ํด๋ผ์ด์–ธํŠธ๋Š” ์ฟ ํ‚ค๋ฅผ ์ €์žฅ
โ†’ ์ฟ ํ‚ค์˜ ์œ ์ง€์‹œ๊ฐ„์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์€ ์ฟ ํ‚ค๋Š” ๋ธŒ๋ผ์šฐ์ € ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ - ๋ธŒ๋ผ์šฐ์ € ์ข…๋ฃŒ์‹œ ์ž๋™ ์†Œ๋ฉธ(idCookie)
โ†’ ์ฟ ํ‚ค์˜ ์œ ์ง€์‹œ๊ฐ„์„ ๋ณ€๊ฒฝํ•œ ์ฟ ํ‚ค๋Š” ์ฟ ํ‚ค ํŒŒ์ผ์— ์ €์žฅ - ์œ ์ง€์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด ์ž๋™ ์†Œ๋ฉธ(countCookie)
๐Ÿ“Œ HttpServletResponse.addCookie(Cookie cookie) : ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ฟ ํ‚ค(Cookie ๊ฐ์ฒด)๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ

response.addCookie(idCookie);
response.addCookie(countCookie);

๐Ÿ“ข Cookie ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•
ํŽ˜์ด์ง€ ์˜ค๋ฅธ์ชฝ ํด๋ฆญ โ†’ ๊ฒ€์‚ฌ โ†’ Application โ†’ Cookie

๐Ÿ“ƒCookieCreateServlet.java

package xyz.itwill.servlet;
//
import java.io.IOException;
import java.io.PrintWriter;
//
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//
//์ฟ ํ‚ค๋ฅผ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌํ•˜๋Š” ์„œ๋ธ”๋ฆฟ
//์ฟ ํ‚ค(Cookie) : ์„œ๋ฒ„(์›นํ”„๋กœ๊ทธ๋žจ)์™€ ํด๋ผ์ด์–ธํŠธ(๋ธŒ๋ผ์šฐ์ €)์˜ ์—ฐ๊ฒฐ ์ง€์†์„ฑ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ๋˜๋Š” ๊ฐ’
//โ†’ ์ฟ ํ‚ค๋Š” ์ ‘์† ์„œ๋ฒ„ ์ •๋ณด๋ฅผ ์‹๋ณ„์ž๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ
@WebServlet("/create.itwill")
public class CookieCreateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	//
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		//
		//1.Cookie ํด๋ž˜์Šค๋กœ ๊ฐ์ฒด ์ƒ์„ฑ
		//โ†’ Cookie ๊ฐ์ฒด : ์ฟ ํ‚ค ๊ด€๋ จ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด
		//Cookie(String name, String Value) : ์ฟ ํ‚ค๋ช…๊ณผ ์ฟ ํ‚ค๊ฐ’์„ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ „๋‹ฌํ•˜์—ฌ ๊ฐ์ฒด ์ƒ์„ฑ
		//โ†’ ์ฟ ํ‚ค๋ช… : ์ฟ ํ‚ค๊ฐ’์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ ์‹๋ณ„์ž
		//โ†’ ์ฟ ํ‚ค๊ฐ’ : ์—ฐ๊ฒฐ ์ง€์†์„ฑ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ๋ฌธ์ž๊ฐ’
		//โ†’ ์ฟ ํ‚ค๋ช…๊ณผ ์ฟ ํ‚ค๊ฐ’์€ ์˜๋ฌธ์ž,์ˆซ์ž,์ผ๋ถ€ ํŠน์ˆ˜๋ฌธ์ž๋งŒ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑ ๊ฐ€๋Šฅ
		Cookie idCookie=new Cookie("id", "abc123");//์ž๋ฐ”์—์„œ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค์‚ฌ์šฉ(์ฒซ๋ฒˆ์งธ๊ฑฐ)
		Cookie countCookie=new Cookie("count", "0");
		//
		//2.ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌ๋˜์–ด ์ €์žฅ๋  ์ฟ ํ‚ค์˜ ์œ ์ง€์‹œ๊ฐ„ ๋ณ€๊ฒฝ
		//Cookie.setMaxAge(int expiry) : ์ฟ ํ‚ค์˜ ์œ ์ง€์‹œ๊ฐ„(์ดˆ)์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฉ”์†Œ๋“œ
		//์ฟ ํ‚ค์˜ ์œ ์ง€์‹œ๊ฐ„์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’์€ [-1๋กœ] ์ž๋™ ์„ค์ •
		//โ†’ ์ฟ ํ‚ค์˜ ์œ ์ง€์‹œ๊ฐ„์ด [-1]๋กœ ์„ค์ •๋œ ๊ฒฝ์šฐ ์ฟ ํ‚ค๋Š” ๋ธŒ๋ผ์šฐ์ € ์ข…๋ฃŒ์‹œ ์ž๋™ ์†Œ๋ฉธ
		countCookie.setMaxAge(24*60*60);//์ฟ ํ‚ค ์œ ์ง€์‹œ๊ฐ„ : 1์ผ
		//
		//3.ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ฟ ํ‚ค๋ฅผ ์ „๋‹ฌ - ์ฟ ํ‚ค๋ฅผ ์ „๋‹ฌ ๋ฐ›์€ ํด๋ผ์ด์–ธํŠธ๋Š” ์ฟ ํ‚ค๋ฅผ ์ €์žฅ
		//โ†’ ์ฟ ํ‚ค์˜ ์œ ์ง€์‹œ๊ฐ„์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์€ ์ฟ ํ‚ค๋Š” ๋ธŒ๋ผ์šฐ์ € ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ - ๋ธŒ๋ผ์šฐ์ € ์ข…๋ฃŒ์‹œ ์ž๋™ ์†Œ๋ฉธ(idCookie)
		//โ†’ ์ฟ ํ‚ค์˜ ์œ ์ง€์‹œ๊ฐ„์„ ๋ณ€๊ฒฝํ•œ ์ฟ ํ‚ค๋Š” ์ฟ ํ‚ค ํŒŒ์ผ์— ์ €์žฅ - ์œ ์ง€์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด ์ž๋™ ์†Œ๋ฉธ(countCookie)
		//HttpServletResponse.addCookie(Cookie cookie) : ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ฟ ํ‚ค(Cookie ๊ฐ์ฒด)๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ
		response.addCookie(idCookie);
		response.addCookie(countCookie);
		//
		out.println("<!DOCTYPE html>");
		out.println("<html>");
		out.println("<head>");
		out.println("<meta charset='UTF-8'>");
		out.println("<title>Servlet</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>์ฟ ํ‚ค ์ „๋‹ฌ</h1>");
		out.println("<hr>");
		out.println("<p>๋„ค ์•ˆ์— ์ฟ ํ‚ค ์žˆ๋‹ค.</p>");
		out.println("<hr>");
		out.println("<a href='read.itwill'>์ฟ ํ‚ค ์ฝ๊ธฐ</a>");
		out.println("</body>");
		out.println("</html>");
	}
}

๐Ÿงํด๋ผ์ด์–ธํŠธ์— ์˜ํ•ด ์ „๋‹ฌ๋œ ์ฟ ํ‚ค๋ฅผ ์ฝ์–ด ์ฟ ํ‚ค๊ฐ’์„ ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌํ•˜๋Š” ์„œ๋ธ”๋ฆฟ

โ†’ ํด๋ผ์ด์–ธํŠธ๋Š” ์š”์ฒญ URL ์ฃผ์†Œ์˜ ์ ‘์† ์„œ๋ฒ„ ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•ด๋‹น ์„œ๋ฒ„ ์ •๋ณด๋กœ ์ €์žฅ๋œ ๋ชจ๋“  ์ฟ ํ‚ค๋ฅผ ์›นํ”„๋กœ๊ทธ๋žจ ์š”์ฒญ์‹œ ๋ฌด์กฐ๊ฑด ์ „๋‹ฌ

๐Ÿ“ข 1.ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์˜จ ๋ชจ๋“  ์ฟ ํ‚ค๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์•„ ์ €์žฅ
๐Ÿ“Œ HttpServletRequest.getCookies() : ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์˜จ ๋ชจ๋“  ์ฟ ํ‚ค๋ฅผ Cookie ๊ฐ์ฒด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

Cookie[] cookies=request.getCookies();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset='UTF-8'>");
out.println("<title>Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>์ฟ ํ‚ค ์ฝ๊ธฐ</h1>");
out.println("<hr>");

๐Ÿ“ข ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์˜จ ์ฟ ํ‚ค๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ

if(cookies==null) {//ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์˜จ ์ฟ ํ‚ค๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ
	out.println("<p>๋„ค ์•ˆ์— ์ฟ ํ‚ค ์—†๋‹ค.</p>");
} else {
	String id="";
	String count="";

๐Ÿ“ข Cookie ๊ฐ์ฒด ๋ฐฐ์—ด์— ์ €์žฅ๋œ ์š”์†Œ(Cookie ๊ฐ์ฒด)๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ œ๊ณต๋ฐ›์•„ ์ฒ˜๋ฆฌ

	for(Cookie cookie:cookies) {

๐Ÿ“ข ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์˜จ ์ฟ ํ‚ค๋“ค ์ค‘ ์ฟ ํ‚ค๋ช…์„ ๋น„๊ตํ•˜์—ฌ ์›ํ•˜๋Š” ์ฟ ํ‚ค๊ฐ’์„ ๋ฐ˜ํ™˜๋ฐ›์•„ ์ €์žฅ
๐Ÿ“Œ Cookie.getName() : Cookie ๊ฐ์ฒด์— ์ €์žฅ๋œ ์ฟ ํ‚ค๋ช…์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
๐Ÿ“Œ Cookie.getValue() : Cookie ๊ฐ์ฒด์— ์ €์žฅ๋œ ์ฟ ํ‚ค๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

	if(cookie.getName().equals("id")) {
		id=cookie.getValue();
	} else if(cookie.getName().equals("count")) {
		count=cookie.getValue();
	}
}

๐Ÿ“ข ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ฟ ํ‚ค๊ฐ’์„ ์ „๋‹ฌํ•˜์—ฌ ์ถœ๋ ฅ

	if(!id.equals("")) {//์ฟ ํ‚ค๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ
		out.println("<p>์•„์ด๋”” = "+id+"</p>");
	}
	if(!count.equals("")) {//์ฟ ํ‚ค๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ
		int cnt=Integer.parseInt(count)+1;//๋ฌธ์ž์—ด์„ ์ˆซ์ž๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ
		out.println("<p>์„œ๋ธ”๋ฆฟ ์š”์ฒญํšŸ์ˆ˜ = "+cnt+"</p>");

๐Ÿ“ข ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ๋œ ์ฟ ํ‚ค์˜ ์ด๋ฆ„๊ณผ ๊ฐ™์€ ์ด๋ฆ„์˜ ์ฟ ํ‚ค๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ์ฟ ํ‚ค๊ฐ’ ๋ณ€๊ฒฝ

		Cookie cookie=new Cookie("count", cnt+"");
		cookie.setMaxAge(24*60*60);
		response.addCookie(cookie);
	}
}
out.println("<hr>");
out.println("<p><a href='create.itwill'>์ฟ ํ‚ค ์ „๋‹ฌ</a></p>");
out.println("<p><a href='remove.itwill'>์ฟ ํ‚ค ์ œ๊ฑฐ</a></p>");
out.println("</body>");
out.println("</html>");

๐Ÿ“ƒCookieReadServlet

package xyz.itwill.servlet;
//
import java.io.IOException;
import java.io.PrintWriter;
//
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//
//ํด๋ผ์ด์–ธํŠธ์— ์˜ํ•ด ์ „๋‹ฌ๋œ ์ฟ ํ‚ค๋ฅผ ์ฝ์–ด ์ฟ ํ‚ค๊ฐ’์„ ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌํ•˜๋Š” ์„œ๋ธ”๋ฆฟ
//โ†’ ํด๋ผ์ด์–ธํŠธ๋Š” ์š”์ฒญ URL ์ฃผ์†Œ์˜ ์ ‘์† ์„œ๋ฒ„ ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•ด๋‹น ์„œ๋ฒ„ ์ •๋ณด๋กœ ์ €์žฅ๋œ ๋ชจ๋“  ์ฟ ํ‚ค๋ฅผ ์›นํ”„๋กœ๊ทธ๋žจ ์š”์ฒญ์‹œ ๋ฌด์กฐ๊ฑด ์ „๋‹ฌ
@WebServlet("/read.itwill")
public class CookieReadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	//
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		//
		//1.ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์˜จ ๋ชจ๋“  ์ฟ ํ‚ค๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์•„ ์ €์žฅ
		//HttpServletRequest.getCookies() : ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์˜จ ๋ชจ๋“  ์ฟ ํ‚ค๋ฅผ Cookie ๊ฐ์ฒด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
		Cookie[] cookies=request.getCookies();
		//
		out.println("<!DOCTYPE html>");
		out.println("<html>");
		out.println("<head>");
		out.println("<meta charset='UTF-8'>");
		out.println("<title>Servlet</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>์ฟ ํ‚ค ์ฝ๊ธฐ</h1>");
		out.println("<hr>");
		if(cookies==null) {//ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์˜จ ์ฟ ํ‚ค๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ
			out.println("<p>๋„ค ์•ˆ์— ์ฟ ํ‚ค ์—†๋‹ค.</p>");
		} else {
			String id="";
			String count="";
			//
			//Cookie ๊ฐ์ฒด ๋ฐฐ์—ด์— ์ €์žฅ๋œ ์š”์†Œ(Cookie ๊ฐ์ฒด)๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ œ๊ณต๋ฐ›์•„ ์ฒ˜๋ฆฌ
			for(Cookie cookie:cookies) {
				//ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์˜จ ์ฟ ํ‚ค๋“ค ์ค‘ ์ฟ ํ‚ค๋ช…์„ ๋น„๊ตํ•˜์—ฌ ์›ํ•˜๋Š” ์ฟ ํ‚ค๊ฐ’์„ ๋ฐ˜ํ™˜๋ฐ›์•„ ์ €์žฅ
				//Cookie.getName() : Cookie ๊ฐ์ฒด์— ์ €์žฅ๋œ ์ฟ ํ‚ค๋ช…์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
				if(cookie.getName().equals("id")) {
					//Cookie.getValue() : Cookie ๊ฐ์ฒด์— ์ €์žฅ๋œ ์ฟ ํ‚ค๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
					id=cookie.getValue();
				} else if(cookie.getName().equals("count")) {
					count=cookie.getValue();
				}
			}
			//ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ฟ ํ‚ค๊ฐ’์„ ์ „๋‹ฌํ•˜์—ฌ ์ถœ๋ ฅ
			if(!id.equals("")) {//์ฟ ํ‚ค๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ
				out.println("<p>์•„์ด๋”” = "+id+"</p>");
			}
			if(!count.equals("")) {//์ฟ ํ‚ค๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ
				int cnt=Integer.parseInt(count)+1;//๋ฌธ์ž์—ด์„ ์ˆซ์ž๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ
				out.println("<p>์„œ๋ธ”๋ฆฟ ์š”์ฒญํšŸ์ˆ˜ = "+cnt+"</p>");
				//
				//ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ๋œ ์ฟ ํ‚ค์˜ ์ด๋ฆ„๊ณผ ๊ฐ™์€ ์ด๋ฆ„์˜ ์ฟ ํ‚ค๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ์ฟ ํ‚ค๊ฐ’ ๋ณ€๊ฒฝ
				Cookie cookie=new Cookie("count", cnt+"");
				cookie.setMaxAge(24*60*60);
				response.addCookie(cookie);
			}
		}
		out.println("<hr>");
		out.println("<p><a href='create.itwill'>์ฟ ํ‚ค ์ „๋‹ฌ</a></p>");
		out.println("<p><a href='remove.itwill'>์ฟ ํ‚ค ์ œ๊ฑฐ</a></p>");
		out.println("</body>");
		out.println("</html>");
	}
}

๐Ÿงํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ๋œ ๋ชจ๋“  ์ฟ ํ‚ค๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ์„œ๋ธ”๋ฆฟ

โ†’ ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ๋œ ์ฟ ํ‚ค์˜ ์œ ์ง€์‹œ๊ฐ„์„ [0]์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ์ฟ ํ‚ค ์ œ๊ฑฐ

๐Ÿ“ข 1. ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์˜จ ๋ชจ๋“  ์ฟ ํ‚ค๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์•„ ์ €์žฅ

Cookie[] cookies=request.getCookies();

๐Ÿ“ข ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์˜จ ์ฟ ํ‚ค๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ

if(cookies!=null) {

๐Ÿ“ข Cookie ๊ฐ์ฒด ๋ฐฐ์—ด์— ์ €์žฅ๋œ ์š”์†Œ๊ฐ’(Cookie ๊ฐ์ฒด)๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ œ๊ณต๋ฐ›์•„ ์ฒ˜๋ฆฌ

	for(Cookie cookie:cookies) {

๐Ÿ“ข ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์˜จ ์ฟ ํ‚ค์˜ ์œ ์ง€์‹œ๊ฐ„์„ [0]์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌ
โ†’ ์œ ์ง€์‹œ๊ฐ„์ด ์ง€๋‚œ ์ฟ ํ‚ค๋Š” ํด๋ผ์ด์–ธํŠธ์—์„œ ์ž๋™ ์†Œ๋ฉธ - ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ๋œ ์ฟ ํ‚ค ์ œ๊ฑฐ

		cookie.setMaxAge(0);
        response.addCookie(cookie);
	}
}

๐Ÿ“ƒCookieRemoveServlet

package xyz.itwill.servlet;
//
import java.io.IOException;
import java.io.PrintWriter;
//
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//
//ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ๋œ ๋ชจ๋“  ์ฟ ํ‚ค๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ์„œ๋ธ”๋ฆฟ
//โ†’ ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ๋œ ์ฟ ํ‚ค์˜ ์œ ์ง€์‹œ๊ฐ„์„ [0]์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ์ฟ ํ‚ค ์ œ๊ฑฐ
@WebServlet("/remove.itwill")
public class CookieRemoveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	//
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		//
		//1. ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์˜จ ๋ชจ๋“  ์ฟ ํ‚ค๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์•„ ์ €์žฅ
		Cookie[] cookies=request.getCookies();
		//
		if(cookies!=null) {//ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์˜จ ์ฟ ํ‚ค๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ
			//Cookie ๊ฐ์ฒด ๋ฐฐ์—ด์— ์ €์žฅ๋œ ์š”์†Œ๊ฐ’(Cookie ๊ฐ์ฒด)๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ œ๊ณต๋ฐ›์•„ ์ฒ˜๋ฆฌ
			for(Cookie cookie:cookies) {
				//ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์˜จ ์ฟ ํ‚ค์˜ ์œ ์ง€์‹œ๊ฐ„์„ [0]์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌ
				//โ†’ ์œ ์ง€์‹œ๊ฐ„์ด ์ง€๋‚œ ์ฟ ํ‚ค๋Š” ํด๋ผ์ด์–ธํŠธ์—์„œ ์ž๋™ ์†Œ๋ฉธ - ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ๋œ ์ฟ ํ‚ค ์ œ๊ฑฐ
				cookie.setMaxAge(0);
				response.addCookie(cookie);
			}
		}
		out.println("<!DOCTYPE html>");
		out.println("<html>");
		out.println("<head>");
		out.println("<meta charset='UTF-8'>");
		out.println("<title>Servlet</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>์ฟ ํ‚ค ์ œ๊ฑฐ</h1>");
		out.println("<hr>");
		out.println("<p>๋„ค ์•ˆ์— ์ฟ ํ‚ค ์—†๋‹ค.</p>");
		out.println("<hr>");
		out.println("<a href='read.itwill'>์ฟ ํ‚ค ์ฝ๊ธฐ</a>");
		out.println("</body>");
		out.println("</html>");
	}
}

0๊ฐœ์˜ ๋Œ“๊ธ€