[Servlet] Cookie, Session

ํ˜œ์ฟฐยท2023๋…„ 8์›” 18์ผ
0
post-thumbnail

๐Ÿ“Œ Cookie

๐Ÿงฉ ๊ฐœ๋…

์ฟ ํ‚ค๋Š” ํด๋ผ์ด์–ธํŠธ์— ํ‚ค์™€ ๊ฐ’ ํ˜•ํƒœ๋กœ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์ž‘์€ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์ด๋‹ค. ์ฟ ํ‚ค๋Š” ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„ ํ†ต์‹  ์ƒํƒœ๊ฐ€ ๊ณ„์† ์œ ์ง€๋˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๊ธฐ ์œ„ํ•ด ์“ฐ์ธ๋‹ค. ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ ์ปดํ“จํ„ฐ์— ํŠน์ • ์ •๋ณด๋ฅผ ์ €์žฅํ•œ๋‹ค.

์ฟ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” Connectionless(๋น„์—ฐ๊ฒฐ์„ฑ)๊ณผ Stateless(๋ฌด์ƒํƒœ)์ด๋‹ค.
Connectionless(๋น„์—ฐ๊ฒฐ์„ฑ) : HTTP๋Š” TCP ์—ฐ๊ฒฐ์„ ๋งบ๊ณ  ์š”์ฒญ(Request)์„ ๋ณด๋‚ด๋ฉด ์„œ๋ฒ„๋Š” ์‘๋‹ต(Response)์„ ๋ณด๋‚ด๊ณ  ์—ฐ๊ฒฐ์ด ๋Š์–ด์ง„๋‹ค.
Stateless(๋ฌด์ƒํƒœ) : HTTP๋Š” ์ƒํƒœ๋ฅผ ๋”ฐ๋กœ ์ €์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ฆ‰, ์—ฐ๊ฒฐ์ด ๋Š์–ด์ง€๋Š” ์ˆœ๊ฐ„ ๋ชจ๋“  ์ƒํƒœ ์ •๋ณด๊ฐ€ ์‚ฌ๋ผ์ง€๊ฒŒ ๋œ๋‹ค.
๋”ฐ๋ผ์„œ ์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ฒซ ๋ฒˆ์งธ ํ†ต์‹  ๋•Œ ๋ณด๋‚ธ ์ •๋ณด๋ฅผ ๋‘ ๋ฒˆ์งธ ํ†ต์‹  ๋•Œ ์•Œ ์ˆ˜ ์—†๋‹ค.
์ฟ ํ‚ค๋Š” ์‹ ๋ขฐ๋„๊ฐ€ ๋–จ์–ด์ง„๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.


์œ„๋Š” ์ฟ ํ‚ค๋ฅผ ํฌ๋กฌ์—์„œ ํ™•์ธํ•œ ๊ฒƒ์ด๋‹ค.

๐Ÿงฉ ํ™œ์šฉ

Cookie๋ฅผ ์ด์šฉํ•ด์„œ ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ, ์‡ผํ•‘๋ชฐ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ฒ˜๋ฆฌ, ๋ฐฉ๋ฌธ ํ™•์ธ ๋“ฑ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
๊ทธ ์ค‘, ๋‚˜๋Š” ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์•˜๋‹ค.


package pack;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;

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;

@WebServlet("/CookieTest")
public class CookieTest extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// ๋กœ๊ทธ์ธ์œผ๋กœ ์ฟ ํ‚ค ์—ฐ์Šต
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("<html><body>");
		// ์ฟ ํ‚ค๊ฐ€ ์—†์€ ๊ฒฝ์šฐ์—๋งŒ ๋กœ๊ทธ์ธ ํ™”๋ฉด ์ถœ๋ ฅ
		String id = null;
		String pwd = null;
		try {
			Cookie[] cookies = request.getCookies(); // ํด๋ผ์ด์–ธํŠธ ์ฟ ํ‚ค๋ฅผ ๋ชจ๋‘ ์ฝ์Œ
			for(int i=0; i<cookies.length; i++) {
				String name = cookies[i].getName();
				System.out.println("์ฝ์€ ์ฟ ํ‚ค name :"+name);
				if(name.equals("id")) {
					id = URLDecoder.decode(cookies[i].getValue(), "utf-8");
				}
				if(name.equals("pwd")) {
					pwd = URLDecoder.decode(cookies[i].getValue(), "utf-8");
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		if(id != null && pwd != null) {
			out.println(id+"๋‹˜ ์ฟ ํ‚ค๋ฅผ ํ†ตํ•ด ๋กœ๊ทธ์ธ ์ƒํƒœ์ž…๋‹ˆ๋‹ค");
			out.println("</body></html>");
			out.close();
			return;
		}
		out.println("* ๋กœ๊ทธ์ธ *");
		out.println("<form method='post'>");
		out.println("id : <input type='text' name='id'><br/>");
		out.println("pwd : <input type='text' name='pwd'><br/>");
		out.println("<input type='submit' value='๋กœ๊ทธ์ธ'>");
		out.println("</form>");
		out.println("</body></html>");
		out.close();
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		String id = request.getParameter("id");
		String pwd = request.getParameter("pwd");
		System.out.println(id+" "+pwd);
		
		if(id.equals("aa")&&pwd.equals("11")) {
			try {
				// ๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ•˜๋ฉด ์ฟ ํ‚ค ์ž‘์„ฑ ํ›„ ํด๋ผ์ด์–ธํŠธ ์ปด์— ์ €์žฅ
				id = URLEncoder.encode(id, "utf-8");
				Cookie idCookie = new Cookie("id", id);
				idCookie.setMaxAge(60*60*24*365);
				
				pwd = URLEncoder.encode(pwd, "utf-8");
				Cookie pwdCookie = new Cookie("pwd", pwd);
				pwdCookie.setMaxAge(60*60*24*365);
				
				response.addCookie(idCookie); // ํด๋ผ์ด์–ธํŠธ ์ปด์— ์•„์ด๋”” ์ €์žฅ
				response.addCookie(pwdCookie); // ํด๋ผ์ด์–ธํŠธ ์ปด์— ๋น„๋ฐ€๋ฒˆํ˜ธ ์ €์žฅ
				
				out.println("๋กœ๊ทธ์ธ ์„ฑ๊ณต - ์ฟ ํ‚ค๊ฐ€ ์„ค์ •๋จ");
			} catch (Exception e) {
				System.out.println("์ฟ ํ‚ค ์ž‘์„ฑ ์‹คํŒจ :"+e);
			}
		}else {
			out.println("๋กœ๊ทธ์ธ ์‹คํŒจ");
		}
	
	}


}

if(id.equals("aa")&&pwd.equals("11")) ์ด ๋ฉ”์„œ๋“œ์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด id๋Š” aa ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” 11๋กœ ๊ฐ„๋‹จํžˆ ์„ค์ •ํ•ด ๋ณด์•˜๋‹ค. ์„œ๋ฒ„๋ฅผ ์ด์šฉํ•ด ์•Œ๋งž์€ ์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ฟ ํ‚ค๊ฐ€ ์ƒ๊ธฐ๊ณ  ์•Œ๋งž์ง€ ์•Š์€ ์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ฟ ํ‚ค๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๋Š”๋‹ค.

๐Ÿ“Œ Session

๐Ÿงฉ ๊ฐœ๋…

์ผ์ •๊ธฐ๊ฐ„ ๋™์•ˆ ๊ฐ™์€ ์‚ฌ์šฉ์ž(ํด๋ผ์ด์–ธํŠธ)๋กœ ๋ถ€ํ„ฐ ๋“ค์–ด์˜ค๋Š” ์ผ๋ จ์˜ ์š”๊ตฌ๋ฅผ ํ•˜๋‚˜์˜ ์ƒํƒœ๋กœ ๋ณด๊ณ  ๊ทธ ์ƒํƒœ๋ฅผ ์ผ์ •ํ•˜๊ฒŒ ์œ ์ง€์‹œํ‚ค๋Š” ๊ธฐ์ˆ ์ด๋‹ค.
์„ธ์…˜์€ ์ฟ ํ‚ค์™€ ๋‹ฌ๋ฆฌ ๋ฐฉ๋ฌธ์ž์˜ ์š”์ฒญ์— ๋”ฐ๋ฅธ ์ •๋ณด๋ฅผ ๋ฐฉ๋ฌธ์ž ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์›น ์„œ๋ฒ„๊ฐ€ ์„ธ์…˜ ์•„์ด๋”” ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ์„œ๋น„์Šค๊ฐ€ ๋Œ์•„๊ฐ€๊ณ  ์žˆ๋Š” ์„œ๋ฒ„์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.
์„ธ์…˜๋„ key value ํ˜•ํƒœ์ด๋‹ค. ๋ณดํ†ต ์„ธ์…˜์˜ ๋งŒ๋ฃŒ๊ธฐ๊ฐ„์€ 30๋ถ„์ด์ง€๋งŒ(default) ์ง์ ‘ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. web.xml์—์„œ ์„ค์ •ํ•˜๋ฉด ์ „์ฒด์˜ ์„ธ์„ ๊ธฐ๊ฐ„์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ณ  ๊ฐ๊ฐ์˜ servlet๋งˆ๋‹ค๋„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒˆ๋กœ ๊ณ ์นจํ•˜๋ฉด ์นด์šดํŠธ๋ฅผ ๋‹ค์‹œ ์‹œ์ž‘ํ•œ๋‹ค.

๐Ÿงฉ ํ™œ์šฉ

์„ธ์…˜์„ ์ด์šฉํ•ด ๋ฐฉ๋ช…๋ก, ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋“ฑ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“Œ Cookie์™€ Session์˜ ์ฐจ์ด์ 

CookieSession
ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ์„œ๋ฒ„์— ์ €์žฅ
ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ๋˜๋ฏ€๋กœ ์„œ๋ฒ„์˜ ์ž์›์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ด์ ์„œ๋ฒ„์— ์ €์žฅ๋˜๋ฏ€๋กœ ์„œ๋ฒ„์˜ ์ž์›์„ ์‚ฌ์šฉ
์„œ๋ฒ„์— ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋งŽ์„ ๊ฒฝ์šฐ ๋ถ€ํ•˜๊ฐ€ ์‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.
์‹ ๋ขฐ๋„๊ฐ€ ๋–จ์–ด์ง„๋‹ค.๋ณด์•ˆ๋ฉด์—์„œ ๋” ์šฐ์ˆ˜
๋งŒ๋ฃŒ์‹œ๊ฐ„ ๋™์•ˆ ํŒŒ์ผ๋กœ ์ €์žฅ๋˜๋ฏ€๋กœ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ข…๋ฃŒํ•ด๋„ ์ •๋ณด๊ฐ€ ๋‚จ์•„์žˆ๋‹ค.๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ๋งŒ๋ฃŒ์‹œ๊ฐ„์— ์ƒ๊ด€์—†์ด ์‚ญ์ œ๋œ๋‹ค.

์ฐธ๊ณ 
https://code-lab1.tistory.com/298
https://dololak.tistory.com/534
https://velog.io/@whwodgns/JS-Session%EC%9D%B4%EB%9E%80

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