Cookie & Session

์ƒํƒœ์œ ์ง€ (Cookie & Session)


Web์˜ ๊ธฐ์ˆ ์€ ํด๋ผ์ด์–ธํŠธ๋ฅผ ํ†ตํ•ด ๋“ค์–ด์˜จ ์ •๋ณด๋ฅผ ์ ‘์†ํ›„ ํ”„๋กœ์„ธ์„œ ์ข…๋ฃŒ์‹œ ์ ‘์†์„ ์ข…๋ฃŒํ•˜๋Š” ํŠน์„ฑ์„ ๊ฐ€์ง€๊ณ ์žˆ๋‹ค.
์ด๋Ÿฌํ•œ ํŠน์„ฑ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์žฅ์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ Web๊ธฐ์ˆ ์˜ ๋ฐœ๋‹ฌํ•จ์œผ๋กœ์จ Web์˜ ์ด๋Ÿฌํ•œ ํŠน์„ฑ์ด ๋‹จ์ ์œผ๋กœ ์ž‘์šฉํ•˜๋Š”๋ฐ
์ƒํƒœ์œ ์ง€๋ฅผ ์œ„ํ•œ ๊ธฐ์ˆ ๋กœ Cookie & Session์ด๊ฐ€ ๋“ฑ์žฅํ–ˆ๋‹ค.

Cookie

  • ํด๋ผ์ด์–ธํŠธ ์ปดํ“จํ„ฐ์— ์ €์žฅํ•˜๋Š” ๊ธฐ์ˆ 
  • ์ €์žฅ๋œ ์ •๋ณด๋ฅผ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ๋˜ํ•œ ์‰ฝ๊ฒŒ ์ ‘๊ทผ๊ฐ€๋Šฅํ•œ ๋‹จ์ ์„ ๊ฐ€์ง„๋‹ค.
  • ์œ ํšจ์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด ์‚ฌ๋ผ์ง„๋‹ค.
  • ์„œ๋ฒ„ ์ •๋ณด์— ์ ‘๊ทผ์‹œ ์‹๋ณ„์„ฑ์„ ๊ฐ€์ง€์ง€ ์•Š์•„ ์›ํ•˜๋Š” ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ธฐ ์–ด๋ ค์›€

Session

  • ์„œ๋ฒ„์— ์ €์žฅํ•˜๋Š” ๊ธฐ์ˆ 
  • ์„œ๋ฒ„๊ฐ€ ์ข…๋ฃŒ๋˜๊ฑฐ๋‚˜ ์œ ํšจ์‹œ์ด ์ง€๋‚˜๋ฉด ์‚ฌ๋ผ์ง„๋‹ค.
  • ํŠน๋ณ„ํ•œ ์‹๋ณ„ID๋ฅผ ๊ฐ€์ง์œผ๋กœ์จ ์„œ๋ฒ„์—์„œ ์›ํ•˜๋Š” ์ •๋ณด๋งŒ์„ ๊ฐ€์งˆ์ˆ˜ ์žˆ๋‹ค.

๋™์ž‘์˜ ์ดํ•ด img

  1. ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  request์‹œ ์ž…๋ ฅ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๋Š” ์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑํ•˜๋Š”๋ฐ Key์™€ Value์ธ Map๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„๋‹ค.
  2. respone์‹œ ๋งŒ๋“ค์–ด์ง„ ์ฟ ํ‚ค์™€ ํ•จ๊ป˜ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ณด๋‚ด์ง€๋ฉฐ ์ฟ ํ‚ค๋Š” ํด๋ผ์ด์–ธํŠธ ์ปดํ“จํ„ฐ์— ์ €์žฅ๋œ๋‹ค.
  3. ๋‹ค์‹œ request์‹œ ๋งŒ๋“ค์–ด์ง„ ์ฟ ํ‚ค๋ฅผ ํฌํ•จํ•˜์—ฌ ๊ฐ™์ด ์„œ๋ฒ„๋กœ ๋ณด๋‚ด์ง„๋‹ค.
  4. ๋งŒ์•ฝ ๋™์ผํ•œ ์ฟ ํ‚ค๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๊ทธ ์ฟ ํ‚ค์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ๋œ๋‹ค.

์ž๋ฐ”์—์„œ Cookie ์ƒ์„ฑ

//Cookie ์ƒ์„ฑ(name=value) :: ํ•œ๊ธ€ ์ธ์ฝ”๋”ฉ ํ›„ ์ €์žฅ
		Cookie cookie = new Cookie("name",URLEncoder.encode("ํ™๊ธธ๋™"));

        cookie.setMaxAge(60*60);	//cookie ์œ ํšจ๊ธฐ๊ฐ„(์ดˆ)  ์ง€๋‚˜๋ฉด ์ฟ ํ‚ค ์‚ญ์ œ
		//cookie.setMaxAge(-1);		//cookie memory ์ €์žฅ        :: ??	 ==> APIํ™•์ธ 
		//cookie.setMaxAge(0);	 	//cookie 0์ดˆ๋™์•ˆ ์œ ํšจ		:: ??	 ==> APIํ™•์ธ 
		res.addCookie(cookie);		//Client ๋กœ response ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉ cookie ์ „์†ก
				
        out.println("<html><body>");
		out.println("Cookie ์ €์žฅ ์™„๋ฃŒ");
		out.println("</body></html>");

Cookie์˜ ์ •๋ณด

// Client ๋กœ ๋ถ€ํ„ฐ ์ „์†ก๋œ Cookie ์ฒ˜๋ฆฌ
		Cookie[] cookies = req.getCookies();
		// Cookie ์˜ name = value ์ฒ˜๋ฆฌ ๋ณ€์ˆ˜ (Map๊ตฌ์กฐ)
		String userName =null;

		//Cookie ์˜ ์กด์žฌ์œ ๋ฌด ๋ฐ name=value ์ฒ˜๋ฆฌ
		if(cookies != null) {
            out.println("Client์—์„œ ์ „์†ก๋œ Cookie ์žˆ์Šต๋‹ˆ๋‹ค.<br/>");
			//Array ๋กœ return  :: Array ๊ฐฏ์ˆ˜๋งŒํผ ์ฒ˜๋ฆฌ
			for(int i=0;i<cookies.length;i++){
				//name = value ํ˜•์‹์˜ ์ €์žฅ๊ฐ’ ์ค‘ name ์ถ”์ถœ
				String name = cookies[i].getName();
				String value = URLDecoder.decode(cookies[i].getValue());
				System.out.println("client๋กœ ๋ถ€ํ„ฐ ์ „์†ก๋œ cookie : "+name+"="+value);
				
				if(name.equals("name")){  // Key๊ฐ’์ค‘ "name"์‹œ value๋กœ userName ์„ค์ •
					userName = value;
				}
			}
		}else{
            out.println("Client์—์„œ ์ „์†ก๋œ cookie๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.<br/>");
        }

		out.println("<html><body>");
		//userName์ด null ์˜๋ฏธ :: cookie ์— id ๊ฐ€ ์ €์žฅ ๋˜์ง€ ์•Š์•˜์Œ ==> ์ฒ˜์Œ๋ฐฉ๋ฌธ
		if(userName == null){
			out.println("์ฒ˜์Œ์ž…๋‹ˆ๋‹ค.");
		}else{
			out.println(userName+"๋‹˜ ํ™˜์˜");
		}
		out.println("</body></html>");
	}

Session

๋™์ž‘์˜ ์ดํ•ด

Cookie๋ฅผ ์ด์šฉํ•˜์—ฌ ์„œ๋ฒ„์— ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์ด Session์ด๋‹ค. img

  1. ์ฟ ํ‚ค๊ฐ€ ์„œ๋ฒ„์—์„œ ๋งŒ๋“ค์–ด์งˆ ๋•Œ ํŠน๋ณ„ํ•œ ์‹๋ณ„ID๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์ฟ ํ‚ค์— ํฌํ•จ๋œ๋‹ค.
  2. ์ฟ ํ‚ค์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ respone์‹œ ์ฟ ํ‚ค์™€ ํ•จ๊ป˜ ํด๋ผ์ด์–ธํŠธ๋กœ ๋ณด๋‚ด์ง„๋‹ค.
  3. request์‹œ ์ฟ ํ‚ค์™€ ํ•จ๊ป˜ ์„œ๋ฒ„๋กœ ๋ณด๋‚ด์ง€๋Š”๋ฐ ์ฟ ํ‚ค์—๋Š” ํŠน๋ณ„ํ•œ ์‹๋ณ„ID๊ฐ€ ์žˆ๊ธฐ์—
    ์„œ๋ฒ„์— ์ €์žฅ๋˜์–ด์žˆ๋Š” ์ •๋ณด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

img ์œ„์™€ ๊ฐ™์€ ๋กœ์ง์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

Session์˜ ์ƒ์„ฑ

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		
		HttpSession session = req.getSession(true);
		// true์‹œ ์„ธ์…˜์„ ์ƒ์„ฑํ•œ๋‹ค.
		if(session.isNew()) {   // ์„ธ์…˜์ด ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์กŒ๋‹ค๋ฉด true
			session.setAttribute("name", new String("ํ™๊ธธ๋™")); // Key , Value ๋กœ ์…‹ํŒ…
		}
		req.setCharacterEncoding("EUC_KR");
		res.setContentType("text/html;charset=EUC_KR");
		PrintWriter out = res.getWriter();

		out.println("<html><head></head><body>");
		out.println("<center><h2>SessionUseCookieOne</h2></center>");
		
		System.out.println("\n UNIQUEํ•œ JSESSIONID๋Š” : "+session.getId());
		// ์‹๋ณ„ID๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค
		if (session.isNew()) {
			out.println("์„ธ์…˜์ด ์ƒˆ๋กœ ์ƒ์„ฑ๋จ<br>");
		} else {
			out.println("\n UNIQUEํ•œ JSESSIONID๋Š” : "+session.getId()+"์‚ฌ์šฉ์ค‘");
		}
		out.println("<hr>");
		out.println("<a href='/edu/SessionUseCookieTwo'>๋งํฌ</a>");
		out.println("</body></html>");
	}

Session์˜ ์ •๋ณด

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

		req.setCharacterEncoding("EUC_KR");
		res.setContentType("text/html;charset=EUC_KR");
		PrintWriter out = res.getWriter();

		Cookie[] cookies = req.getCookies();
		if(cookies != null) {
			for(int i = 0; i<cookies.length; i++) {
				System.out.print("\nCookie์— ์ €์žฅ๋œ ์ •๋ณด : ");
				System.out.print(cookies[i].getName()+" : "+cookies[i].getValue());
				System.out.print("์ •๋ณด ํ™•์ธ");
				System.out.print("\n");
			}
		}
		
		HttpSession session = req.getSession(false);
		
		out.println("<html><head></head><body>");
		out.println("<center><h2>SessionUseCookieTwo</h2></center>");

		if (session != null) {
			out.println("<hr> JSESSIONID = "+session.getId()+"<hr>");
			String name = (String)session.getAttribute("name");
			out.println("์ด๋ฆ„ : "+name);
		} else {
			out.println("์ฒ˜์Œ์ด์‹ญ๋‹ˆ๋‹ค.");
		}
		out.println("</body></html>");
	}

Categories:

Updated:

Leave a comment