Java - 18. ์ฟ ํ‚ค๐Ÿช & ์„ธ์…˜

godkimchichiยท2020๋…„ 10์›” 22์ผ
1

๊ณ ๊ธ‰์ž๋ฐ”

๋ชฉ๋ก ๋ณด๊ธฐ
42/47

Cookie vs. Session

CookieSession
ํด๋ผ์ด์–ธํŠธ(๋ธŒ๋ผ์šฐ์ €)์˜ ๋ฉ”๋ชจ๋ฆฌ ๋˜๋Š” ํŒŒ์ผ์— ์ €์žฅ์„œ๋ฒ„ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ
- ํด๋ผ์ด์–ธํŠธ ๋กœ์ปฌ์— ์ €์žฅ๋˜์–ด ๋ณด์•ˆ์— ์ทจ์•ฝ
- ํŠนํžˆ ํŒŒ์ผ๋กœ ์ €์žฅ๋˜๋Š” ๊ฒฝ์šฐ ํƒˆ์ทจ, ๋ณ€์กฐ๋  ์œ„ํ—˜์ด ์žˆ๊ณ  Request/Response์—์„œ ์Šค๋‚˜์ดํ•‘๋‹นํ•  ์œ„ํ—˜์ด ์žˆ์–ด ๋ณด์•ˆ์ด ๋น„๊ต์  ์ทจ์•ฝ
ํด๋ผ์ด์–ธํŠธ ์ •๋ณด ์ž์ฒด๋Š” ์„œ๋ฒ„์— ์ €์žฅ๋˜์–ด์žˆ์œผ๋ฏ€๋กœ ๋น„๊ต์  ์•ˆ์ „ํ•จ
์ง€์† ์ฟ ํ‚ค์˜ ๊ฒฝ์šฐ ๋ธŒ๋ผ์šฐ์ € ์ข…๋ฃŒ์‹œ์—๋„ ์ €์žฅ๋˜์–ด ์žˆ์„ ์ˆ˜ ์žˆ์Œ์„œ๋ฒ„์—์„œ ๋งŒ๋ฃŒ์‹œ๊ฐ„/๋‚ ์งœ๋ฅผ ์ •ํ•ด์„œ ์ง€์›Œ๋ฒ„๋ฆด ์ˆ˜ ์žˆ๊ธฐ๋„ํ•˜๊ณ  ์„ธ์…˜ ์ฟ ํ‚ค์— ์„ธ์…˜ ์•„์ด๋””๋ฅผ ์ •ํ•œ ๊ฒฝ์šฐ, ๋ธŒ๋ผ์šฐ์ € ์ข…๋ฃŒ์‹œ ์„ธ์…˜์•„์ด๋””๊ฐ€ ๋‚ ์•„๊ฐˆ ์ˆ˜ ์žˆ๋‹ค
์ฟ ํ‚ค์— ์ •๋ณด๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ฒ„์— ์š”์ฒญ์‹œ ํ—ค๋”๋ฅผ ๋ฐ”๋กœ ์ฐธ์กฐํ•˜๋ฉด ๋˜๋ฏ€๋กœ ์†๋„์—์„œ ์œ ๋ฆฌํ•จ์ œ๊ณต๋ฐ›์€ ์„ธ์…˜ID(key)๋ฅผ ์ด์šฉํ•ด์„œ ์„œ๋ฒ„์—์„œ ๋‹ค์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ์กฐํ•ด์•ผํ•˜๋ฏ€๋กœ ์†๋„๊ฐ€ ๋น„๊ต์  ๋Š๋ฆผ

Cookie

  • ์›น์„œ๋ฒ„์™€ ๋ธŒ๋ผ์šฐ์ €๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‚ฌ์šฉํ•˜๋Š” ๋™์•ˆ ํ•„์š”ํ•œ ๊ฐ’์„ ์ฟ ํ‚ค๋ฅผ ํ†ตํ•ด ๊ณต์œ ํ•˜์—ฌ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•จ
  • ์‚ฌ์šฉ์ž๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•˜๊ธฐ๋•Œ๋ฌธ์— ์ฟ ํ‚ค๋ฅผ ์ด์šฉํ•จ
    • HTTP๋Š” ๋น„์—ฐ๊ฒฐ์ง€ํ–ฅ: ์š”์ฒญ, ์‘๋‹ต ๊ณผ์ •์ด ๋๋‚˜๋ฉด ์—ฐ๊ฒฐ์ด ๋Š๊ธฐ๊ธฐ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž ํ™•์ธ์ด ์–ด๋ ค์›€
  • ์ฟ ํ‚ค ์ƒ์„ฑํ›„, ์š”์ฒญ URL ๋‚ ๋ฆด๋•Œ๋งˆ๋‹ค URL(์š”์ฒญ)์— ํ•ด๋‹นํ•˜๋Š” ์ €์žฅํ•œ ์ฟ ํ‚ค๋ฅผ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๊ฐ–๊ณ ์žˆ๋‹ค๋ฉด ์š”์ฒญ์ •๋ณด์™€ ํ•จ๊ป˜ ์ฟ ํ‚ค๋ฅผ ์„œ๋ฒ„๋กœ ๋ณด๋ƒ„
    • ์‚ฌ์šฉ์ž๊ฐ€ ์ ‘์†ํ•ด์„œ ๋“ค์–ด์˜ด -> ์„œ๋ฒ„๊ฐ€ ์‚ฌ์šฉ์ž ์ด๋ฆ„์„ ๊ธฐ์–ตํ•˜๊ณ ์‹ถ์Œ
    • ์„œ๋ฒ„ says "hey ๋ธŒ๋ผ์šฐ์ €์•ผ ์‚ฌ์šฉ์ž ์ด๋ฆ„ ์ข€ ์ €์žฅํ•ด์ค˜"
    • ๋ธŒ๋ผ์šฐ์ € says "ok ์ด๋ฆ„ ์ฟ ํ‚ค์— ์ €์žฅํ• ๊ฒŒ"
    • ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์‚ฌ์šฉ์ž ์ด๋ฆ„์„ ์ฟ ํ‚ค์— ์ €์žฅํ•จ
    • ๋ธŒ๋ผ์šฐ์ € says "๋‚˜ #@(F#%B)๋ผ๋Š”๊ฒŒ ํ•„์š”ํ•ด~ ์ด๊ฑฐํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์ฟ ํ‚ค์ •๋ณด๋„ ๊ฐ™์ด ๋ณด๋‚ด์ค„๊ฒŒ"
    • ์„œ๋ฒ„ says "๊ทผ๋ฐ ๋„ˆ๊ฐ€ ๋ˆ„๊ตฌ์•ผ ์•„ ์ฟ ํ‚ค๋ณด๋‚ด์คฌ๊ตฌ๋‚˜ ๊นŒ์„œ ๋ณผ๊ฒŒ~ ์•„ํ•˜ ์ €๋ฒˆ์— ์˜จ ์‚ฌ์šฉ์ž A๋กœ๊ตฐ~"

๊ตฌ์„ฑ์š”์†Œ

  • ์ด๋ฆ„
  • ๊ฐ’
  • ์œ ํšจ์‹œ๊ฐ„(์ดˆ)
  • ๋„๋ฉ”์ธ
    • ex) www.somehost.com, .somehost.com
    • ๋ณด์•ˆ๋•Œ๋ฌธ์— ์ฟ ํ‚ค์˜ ๋„๋ฉ”์ธ์ด ์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑํ•œ ๋„๋ฉ”์ธ์„ ๋ฒ—์–ด๋‚˜๋ฉด ๋ธŒ๋ผ์šฐ์ €๋Š” ์ฟ ํ‚ค๋ฅผ ์ €์žฅ(์ƒ์„ฑ)ํ•˜์ง€ ์•Š๋Š”๋‹ค.
    • ์„ค์ •์•ˆํ•˜๋ฉด ํ†ฐ์บฃ์„œ๋ฒ„ ๊ธฐ๋ณธ์ •๋ณด

๋™์ž‘ ๋ฐฉ์‹

  • ์ฟ ํ‚ค ์ƒ์„ฑ ๋‹จ๊ณ„
    • ์ƒ์„ฑํ•œ ์ฟ ํ‚ค๋ฅผ ์‘๋‹ต๋ฐ์ดํ„ฐ์˜ ํ—ค๋”์— ์ €์žฅํ•˜์—ฌ ์›น๋ธŒ๋ผ์šฐ์ €์— ์ „์†ก
    • ์˜ˆ์‹œ
      • 1) ์‚ฌ์šฉ์ž๊ฐ€ request ๋‚ ๋ฆผ
      • 2) ์„œ๋ฒ„๊ฐ€ responseํ•˜๋Š” ์‹œ์ ์— setCookieHeaeder ๋ผ๋Š”๊ฑธ ํ†ตํ•ด์„œ response
      • 3) ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๊ทธ ์‘๋‹ตheader๋ฅผ ๋ณด๊ณ  '์•„ ์ฟ ํ‚ค๋ฅผ ๋งŒ๋“ค์–ด์•ผํ•˜๊ตฌ๋‚˜' ํ•˜๊ณ  ์ฟ ํ‚ค๋ฅผ ๋งŒ๋“ฌ
  • ์ฟ ํ‚ค ์ €์žฅ ๋‹จ๊ณ„
    • ์›น ๋ธŒ๋ผ์šฐ์ €๋Š” ์‘๋‹ต๋ฐ์ดํ„ฐ์— ํฌํ•จ๋œ ์ฟ ํ‚ค๋ฅผ ์ฟ ํ‚ค ์ €์žฅ์†Œ์— ๋ณด๊ด€
    • ์ฟ ํ‚ค ์ข…๋ฅ˜์— ๋”ฐ๋ผ ๋ฉ”๋ชจ๋ฆฌ๋‚˜ ํŒŒ์ผ์— ์ €์žฅ
  • ์ฟ ํ‚ค ์ „์†ก ๋‹จ๊ณ„
    • ์›น๋ธŒ๋ผ์šฐ์ €๋Š” ์ €์žฅํ•œ ์ฟ ํ‚ค๋ฅผ ์š”์ฒญ์ด ์žˆ์„ ๋•Œ๋งˆ๋‹ค ์›น์„œ๋ฒ„์— ์ „์†ก(์‚ญ์ œ๋˜๊ธฐ์ „๊นŒ์ง€)
    • ์‚ฌ์šฉ์ž๊ฐ€ ์›น์„œ๋ฒ„์— ์š”์ฒญํ• ๋•Œ๋งˆ๋‹ค, ์š”์ฒญํ•œ url์— ๋งž๋Š” ์ €์žฅ๋œ ์œ ํšจํ•œ์ฟ ํ‚ค๊ฐ€์žˆ๋Š”์ง€ ๋ถ€ํ„ฐ ํ™•์ธํ•ด์„œ ๊ทธ ์ฟ ํ‚ค๋ฅผ ์š”์ฒญ์‹œ์— ํ•จ๊ป˜ ๋˜์ง
      • ex) ์ฑ…๊ฐˆํ”ผ๊ธฐ๋Šฅ, 5ํŽ˜์ด์ง€๊นŒ์ง€ ์ฝ์—ˆ๋„ค ์ฟ ํ‚ค์—์ €์žฅ ๋‚˜์ค‘์—์˜ค๋ฉด 5ํŽ˜์ด์ง€๋ถ€ํ„ฐ ํŽด์ ธ์„œ ์ฝ์„ ์ˆ˜ ์žˆ์Œ
    • ์›น์„œ๋ฒ„๋Š” ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ „์†กํ•œ ์ฟ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•„์š”ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰

์˜ˆ์ œ T05_ServletCookieTest

์ฟ ํ‚ค ์„ค์ •

1. ์ฟ ํ‚ค ๊ฐ์ฒด ์ƒ์„ฑ

  • Cookie cookie = new Cookie("key๊ฐ’", "value๊ฐ’");
  • ์‚ฌ์šฉ ๋ถˆ๊ฐ€ ๋ฌธ์ž: ๊ณต๋ฐฑ, =,"/?@:;
  • ์ฟ ํ‚ค๊ฐ’์€ ์‚ฌ์šฉ๋ถˆ๊ฐ€๋ฌธ์ž๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์•„์Šคํ‚ค ๋ฌธ์ž ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ์ด์™ธ์˜ ๊ฐ’(ex: ํ•œ๊ธ€)์„ ์‚ฌ์šฉ์‹œ์—๋Š” URLEncoder.encode()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ฝ”๋”ฉ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค€๋‹ค.

2. ์ฟ ํ‚ค ์ตœ๋Œ€ ์ง€์†์‹œ๊ฐ„ ์„ค์ •

  • cookie.setMaxAge(60*60*24)
    • 24์‹œ๊ฐ„
  • ์ดˆ๋‹จ์œ„
  • ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ข…๋ฃŒํ•  ๋•Œ ์ฟ ํ‚ค๋ฅผ ํ•จ๊ป˜ ์‚ญ์ œํ•œ๋‹ค

3. ์‘๋‹ตํ—ค๋”์— ์ฟ ํ‚ค ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€

  • response.addCookie(cookie);
  • ์‘๋‹ตํ—ค๋”๋ฅผ ํ†ตํ•ด์„œ ๋ธŒ๋ผ์šฐ์ €์— ์ „๋‹ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ถœ๋ ฅ๋ฒ„ํผ๊ฐ€ ํ”Œ๋Ÿฌ์‹œ๋œ ์ดํ›„์—๋Š” ์ฟ ํ‚ค๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์—†๋‹ค
private void setCookieExam(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  // 1. ์ฟ ํ‚ค ์ƒ์„ฑํ•˜๊ธฐ
  Cookie userId = new Cookie("userId", req.getParameter("userId"));

  // 2. ์ฟ ํ‚ค๊ฐ’์— ํ•œ๊ธ€์„ ์‚ฌ์šฉ์‹œ ์ธ์ฝ”๋”ฉ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค€๋‹ค.
  Cookie name = new Cookie("name", URLEncoder.encode(req.getParameter("name"), "utf-8"));

  // 3. ์ฟ ํ‚ค ์†Œ๋ฉธ ์‹œ๊ฐ„ ์„ค์ •(์ดˆ๋‹จ์œ„) => ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์›น๋ธŒ๋ผ์šฐ์ € ์ข…๋ฃŒํ•  ๋•Œ ์ฟ ํ‚ค๋ฅผ ํ•จ๊ป˜ ์‚ญ์ œ
  userId.setMaxAge(60*60*24); // 1์ผ
//name.setMaxAge(60*60*48); // 2์ผ

  // 4. ์‘๋‹ตํ—ค๋”์— ์ฟ ํ‚ค ์ถ”๊ฐ€ํ•˜๊ธฐ
  resp.addCookie(userId);
  resp.addCookie(name);

  // 5. ์‘๋‹ตํ—ค๋”์— ์ธ์ฝ”๋”ฉ ๋ฐ Content-Type ์„ค์ •
  resp.setCharacterEncoding("utf-8");
  resp.setContentType("text/html");

  // 6. ์‘๋‹ต ํ—ค๋”์— ์ถœ๋ ฅ
  PrintWriter out = resp.getWriter();
  String title = "์ฟ ํ‚ค์„ค์ • ์˜ˆ์ œ";
  out.println("<!DOCTYPE html>\n"
            + "<html>\n"
            + "  <head>\n"
            + "    <title>" + title + "</title>"
            +	"  </head>\n"
            + "  <body>\n"
            + "    <h1 align=\"center\">" + title + "</h1>\n"
            + "    <ul>\n"
            + "      <li><b>ID</b>: "
            +		   req.getParameter("userId") + "</li>\n"
            +	"	   <li><b>์ด๋ฆ„</b>:"
            + 	   req.getParameter("name") + "</li>\n"
            + "    </ul>\n"
            + "  </body>\n"
            + "</html>\n");
}

๊ฒฐ๊ณผ

    • ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’: name = ๊ณ ๊ธธ๋™ userId = dooli
    • name: ๊ณ ๊ธธ๋™
      • setMaxAge()ํ•˜์ง€์•Š์•˜๋”๋‹ˆ session์œผ๋กœ ์„ค์ •๋จ (๋ธŒ๋ผ์šฐ์ € ์ข…๋ฃŒ์‹œ ์ข…๋ฃŒ)
      • Cookie name = new Cookie("name", URLEncoder.encode(req.getParameter("name"), "utf-8")); ์ธ์ฝ”๋”ฉ ์ฒ˜๋ฆฌ๋˜์–ด์„œ ํ™”๋ฉด์—๋Š” ์ž˜ ๋ณด์ž„

์ฟ ํ‚ค ์ฝ๊ธฐ

private void readCookieExam(HttpServletRequest req, HttpServletResponse resp) throws IOException {

  Cookie cookie = null;

  // ํ˜„์žฌ ๋„๋ฉ”์ธ์—์„œ ์‚ฌ์šฉ์ค‘์ธ ์ฟ ํ‚ค ์ •๋ณด ๋ฐฐ์—ด ๊ฐ€์ ธ์˜ค๊ธฐ
  Cookie cookies [] = req.getCookies();

  // ์‘๋‹ตํ—ค๋”์— ์ธ์ฝ”๋”ฉ ๋ฐ ContentType ์ง€์ •
  resp.setCharacterEncoding("utf-8");
  resp.setContentType("text/html");

  PrintWriter out = resp.getWriter();
  String title = "์ฟ ํ‚ค ์ •๋ณด ์ฝ๊ธฐ ์˜ˆ์ œ";

  out.println("<!DOCTYPE html>\n"
             + "<html>\n"
             + "<head><title>" + title + "</title></head>\n"
             + "<body>\n");
  if(cookies != null) {
      out.println("<h2>" + title + "</h2>");
      for (int i = 0; i < cookies.length; i++) {
          cookie = cookies[i];
          out.print("name : " + cookie.getName() +"<br>");
          out.print("value : " + URLDecoder.decode(cookie.getValue(), "utf-8") +"<br>");
          out.print("<br>");
      }
  }else {
      out.println("<h2>์ฟ ํ‚ค ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.</h2>");
      out.println("</body");
      out.println("</html");
  }
}

๊ฒฐ๊ณผ

    • ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ข…๋ฃŒํ•˜์ง€ ์•Š์•„์„œ ์ฟ ํ‚ค๊ฐ€ ์•„์ง ๋‚จ์•„์žˆ์Œ
    • ๋ธŒ๋ผ์šฐ์ € ์žฌ์‹œ์ž‘ํ•˜๋‹ˆ setMaxAge๋ฅผ ์„ค์ •ํ•ด์คฌ๋˜ userId๋งŒ ๋‚จ์•„์žˆ์Œ

์ฟ ํ‚ค ์‚ญ์ œ

1. ์‚ฌ์šฉ์ค‘์ธ ์ฟ ํ‚ค ์ •๋ณด๋ฅผ ์ด์šฉํ•ด ์ฟ ํ‚ค ๊ฐ์ฒด ์ƒ์„ฑ

2. ์ฟ ํ‚ค ๊ฐ์ฒด์˜ ์ตœ๋Œ€ ์ง€์†์‹œ๊ฐ„์„ 0์œผ๋กœ ์„ค์ •

3. ์„ค์ •ํ•œ ์ฟ ํ‚ค๊ฐ์ฒด๋ฅผ ์‘๋‹ตํ—ค๋”์— ์ถ”๊ฐ€ํ•˜์—ฌ ์ „์†ก

private void deleteCookieExam(HttpServletRequest req, HttpServletResponse resp) thrwos IOException {

  Cookie cookie = null;

  // ํ˜„์žฌ ๋„๋ฉ”์ธ์—์„œ ์‚ฌ์šฉ์ค‘์ธ ์ฟ ํ‚ค ์ •๋ณด ๋ฐฐ์—ด ๊ฐ€์ ธ์˜ค๊ธฐ
  Cookies[] cookies = req.getCookies();

  // ์‘๋‹ตํ—ค๋”์— ์ธ์ฝ”๋”ฉ ๋ฐ Content-Type์„ค์ •
  resp.setCharacterEncoding("utf-8");
  resp.setContentType("text/html");

  PrintWriter out = resp.getWriter();
  String title = "์ฟ ํ‚ค์ •๋ณด ์‚ญ์ œ ์˜ˆ์ œ";

  out.println("<!DOCTYPE html>\n"
            + "<html>\n"
            + "<head><title> + title + </title></head>\n"
            + "<body>\n");

  if (cookies != null) {
      out.println("<h2>" + title + "</h2>");

      for (int i = 0; i < cookies.length; i++) {
          cookie = cookies[i];

          if(cookie.getName().equals("userId") ) {
              // ์ฟ ํ‚ค ์ œ๊ฑฐํ•˜๊ธฐ
              cookie.setMaxAge(0);
              resp.addCookie(cookie);
              out.print("์‚ญ์ œํ•œ ์ฟ ํ‚ค : " + cookie.getName() + "<br>");
          }
          out.print("name : " + cookie.getName() + ", ");
          out.print("value : " + URLDecoder.decode(cookie.getValue(), "UTF-8"));
      }
  } else {
      out.println("<h2>๊ตญํฌ์ •๋ณด๊ฐ€ ์—…์Šต๋‹ˆ๋‹ค.<h2>");
  }

}

๊ฒฐ๊ณผ

    • ์ฟ ํ‚ค๊ฐ€ ์ž˜ ์‚ญ์ œ๋จ!

Session

  • Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user.
  • ์ฟ ํ‚ค์ฒ˜๋Ÿผ ์‚ฌ๋ผ์ง€์ง€ ์•Š๊ณ  ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
    • ์žฅ: ์ฟ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋ณด๋‹ค ๋ณด์•ˆ์ด ํ–ฅ์ƒ๋œ๋‹ค.
    • ๋‹จ: ์„œ๋ฒ„์˜ ์ž์›์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์†๋„ ์ €ํ•˜๋„ ์˜ฌ ์ˆ˜ ์žˆ์Œ
  • ์„ธ์…˜์€ ์„ธ์…˜ID๋ฅผ ์ด์šฉํ•ด ์‚ฌ์šฉ์ž(์›น๋ธŒ๋ผ์šฐ์ €)๋ณ„๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ์„ธ์…˜ ๊ฐ์ฒด์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” session id๊ฐ€ ํ•„์š”
    • session id๋ฅผ key๊ฐ’์œผ๋กœ ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ๊บผ๋‚ด์˜ฌ ์ˆ˜ ์žˆ์Œ
    • ๋กœ๊ทธ์ธ ์‹œ(= ์‚ฌ์šฉ์ž ์š”์ฒญ ์‹œ) ์‚ฌ์šฉ์ž ๋ณ„๋กœ ํ•„์š”ํ•œ ์ •๋ณด ์ €์žฅ (ex ์„ธ์…˜์ด ๋งŒ๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์žฌ๋กœ๊ทธ์ธํ•ด์ฃผ์„ธ์š” ํ• ๋•Œ ๋ณด๋Š” ๊ทธ ์„ธ์…˜)
  • ์„ธ์…˜์„ ์ด์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์ƒํƒœ๊ฐ’์ด ์ €์žฅ๋˜์ง€ ์•Š์Œ
    • ์ƒˆ๋กœ๊ณ ์นจํ• ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ์‚ฌ๋žŒ์œผ๋กœ ์ธ์‹

์„ธ์…˜ ์ƒ์„ฑ ๋ฐฉ๋ฒ•

  • HttpSession session = request.getSession(boolean๊ฐ’);
    • true: ์„ธ์…˜ ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๋‹ค (์•ˆ๋„ฃ์–ด๋„ true)
    • false: ์„ธ์…˜ ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด null์„ ๋ฆฌํ„ดํ•œ๋‹ค (์„ธ์…˜ ์œ ๋ฌด ํ™•์ธ์‹œ ์“ฐ์ž„)

์„ธ์…˜ ์‚ญ์ œ ๋ฐฉ๋ฒ•

  • invalidate() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
  • setMaxInactiveInterval(int interval) ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
    • ์ผ์ •์‹œ๊ฐ„(์ดˆ)๋™์•ˆ ์š”์ฒญ์ด ์—†์œผ๋ฉด ์„ธ์…˜๊ฐ์ฒด ์‚ญ์ œ๋จ
  • web.xml์— ๏ผœsession-config๏ผž ์„ค์ •ํ•˜๊ธฐ (๋ถ„๋‹จ์œ„๋กœ ์„ค์ •ํ•จ.)

์˜ˆ์ œ

  • T06_ServletSessionTest.java
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

  // ์„ธ์…˜์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐ ์—†์œผ๋ฉด ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๋‹ค
  HttpSession session = req.getSession(true);

  // ์„ธ์…˜ ์‚ญ์ œ
//session.invalidate(); // 1๋ฒˆ ๋ฐฉ๋ฒ•
//session.setMaxInactiveInterval(20); // 2๋ฒˆ ๋ฐฉ๋ฒ•: 20์ดˆ
  // ์ƒ์„ฑ์‹œ๊ฐ„ ๊ฐ€์ ธ์˜ค๊ธฐ
  Date createTime = new Date(session.getCreationTime());

  // ๋งˆ์ง€๋ง‰ ์ ‘๊ทผ์‹œ๊ฐ„ ๊ฐ€์ ธ์˜ค๊ธฐ
  Date lastAccessTime = new Date(session.getLastAccessedTime());

  String title = "์žฌ๋ฐฉ๋ฌธ์„ ํ™˜์˜ํ•˜๋‹ˆ๋‹ค.";
  int visitCount = 0; // ๋ฐฉ๋ฌธํšŸ์ˆ˜
  String userId = "chichi";

  if (session.isNew()) {
    title = "์ฒ˜์Œ ๋ฐฉ๋ฌธ์„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.";
    session.setAttribute("userId", userId); // ํ‚ค๊ฐ’, ๋ฐธ๋ฅ˜๊ฐ’
  }else {
    visitCount = (Integer)session.getAttribute("visitCount"); // ์„ธ์…˜์— ์žˆ๋Š” ๊ธฐ์กด ๊ฐ’
    visitCount++;
    userId = (String) session.getAttribute("userId");
  }

  System.out.println("๋ฐฉ๋ฌธํšŸ์ˆ˜ : " + visitCount); // ์„œ๋ฒ„๋‹จ์—์„œ ํ•œ๋ฒˆ ์ฝ˜์†”์— ์ฐ์–ด๋ณธ๊ฒƒ ํ™”๋ฉด๊ณผ ์ƒ๊ด€x
  session.setAttribute("visitCount", visitCount); // ์„ธ์…˜์— ++๋œ ๋ฐฉ๋ฌธ์ˆ˜ ์ €์žฅ

  // ์‘๋‹ตํ—ค๋” html ์ฝ”๋“œ ์ถœ๋ ฅ
  // ์ƒ๋žต
}

web.xml

  • ์„ธ์…˜ ์‚ญ์ œ๋ฅผ ์œ„ํ•œ ์„ค์ •: ๋ถ„ ๋‹จ์œ„
<session-config>
  <session-timeout>1</session-timeout>
</session-config>

๊ฒฐ๊ณผ

    • ์ฒซ๋ฐฉ๋ฌธ
    • ์žฌ๋ฐฉ๋ฌธ

์ฐธ๊ณ 

profile
๊ฐˆ ๊ธธ์ด ๋ฉ€๋‹ค

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