[JSP] Cookie (์ฟ ํ‚ค)



  • ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์„œ๋ฒ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๋ฉด ์„œ๋ฒ„ ์ธก์—์„œ๋Š” ๋กœ์ง ์ˆ˜ํ–‰, ์›น๋ธŒ๋ผ์šฐ์ €์— ์‘๋‹ตํ•œ ํ›„ ์›น๋ธŒ๋ผ์šฐ์ €์™€์˜ ๊ด€๊ณ„๋ฅผ ์ข…๋ฃŒํ•˜๋Š”๋ฐ, ์ด๋Ÿฌํ•œ ์ƒํƒœ๋ฅผ โ€˜๋ฌด์ƒํƒœ ์„œ๋ฒ„, ๋ฌด์ƒํƒœ ๋กœ์งโ€™์ด๋ผ๊ณ  ํ•œ๋‹ค.

  • ์›น๋ธŒ๋ผ์šฐ์ €์— ์‘๋‹ต ํ›„ ์—ฐ๊ฒฐ์ด ํ•ด์ œ๋˜๋Š” ๊ฒƒ์€ http ํ”„๋กœํ† ์ฝœ์˜ ํŠน์ง•์œผ๋กœ, ์—ฐ๊ฒฐ์ด ๋Š๊ฒผ์„ ๋•Œ ์–ด๋– ํ•œ ์ •๋ณด๋ฅผ ์ง€์†์ ์œผ๋กœ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ์ˆ˜๋‹จ์œผ๋กœ โ€˜์ฟ ํ‚คโ€™๋ฅผ ์‚ฌ์šฉ

  • ์ฟ ํ‚ค๋Š” ์„œ๋ฒ„์—์„œ ์ƒ์„ฑํ•˜๋ฉฐ, ์„œ๋ฒ„๊ฐ€ ์•„๋‹Œ ํด๋ผ์ด์–ธํŠธ ์ธก์— ํŠน์ • ์ •๋ณด๋ฅผ (HDD์— ํ…์ŠคํŠธ ํŒŒ์ผ๋กœ) ์ €์žฅ

  • ์„œ๋ฒ„์— ์š”์ฒญ ํ•  ๋•Œ๋งˆ๋‹ค ์ฟ ํ‚ค์˜ ์†์„ฑ ๊ฐ’์„ ์ฐธ์กฐ, ๋ณ€๊ฒฝ ํ•  ์ˆ˜ ์žˆ์Œ

  • ์ฟ ํ‚ค๋Š” 4kb๋กœ ์šฉ๋Ÿ‰์ด ์ œํ•œ์ ์ด๋ฉฐ, ๋ฐ์ดํ„ฐ ์ •๋ณด๋Š” 300๊ฐœ๊นŒ์ง€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ

  • ๋ณด์•ˆ์— ์ทจ์•ฝํ•˜๋ฏ€๋กœ, ์ค‘์š”ํ•œ ์ •๋ณด๋Š” ์ €์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค. (ex) ๋กœ๊ทธ์ธ ์‹œ ์ฟ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•ด ์•„์ด๋”” ๊ฐ’๋งŒ ์ €์žฅ, ์œ ์ถœ๋˜๋ฉด ์•ˆ๋˜๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ ๋“ฑ์˜ ์ •๋ณด๋Š” ์„ธ์…˜์—์„œ ๋”ฐ๋กœ ๊ด€๋ฆฌ / ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋“ฑ์— ์‚ฌ์šฉ)

  • ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„ํŽ˜์ด์ง€ ์ ‘์† -> ์ž…๋ ฅ ํ›„ ํŽ˜์ด์ง€ ์ „ํ™˜ ์‹œ ์„ธ์…˜ ์ƒ์„ฑ์ฝ”๋“œ๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด ์„ธ์…˜ ์ƒ์„ฑ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฟ ํ‚ค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ -> ์ƒ์„ฑ๋œ ์ฟ ํ‚ค์˜ ํ™•์ธ์šฉ ๋ฐ์ดํ„ฐ(๋น„๊ต ๋ฐ์ดํ„ฐ)๊ฐ€ ์„œ๋ฒ„์— ๋‚จ๊ณ , ์„œ๋ฒ„์— ๋‚จ์•„์žˆ๋Š” ํ™•์ธ์šฉ ๋ฐ์ดํ„ฐ๋ฅผ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ์ค€ ๋ฐ์ดํ„ฐ(=์ฟ ํ‚ค)๋ฅผ ํด๋ผ์ด์–ธํŠธ๋กœ ์ „์†ก

  • ์ฟ ํ‚ค ๊ด€๋ จ ๋ฉ”์†Œ๋“œ

    • setMaxAge(int seconds) : ์ฟ ํ‚ค ๋งŒ๋ฃŒ์‹œ๊ฐ„ ์„ค์ •

    • setPath() : ์ฟ ํ‚ค ์‚ฌ์šฉ์˜ ์œ ํšจ ๋””๋ ‰ํ† ๋ฆฌ ์„ค์ •

    • setValue(String value) : ์ฟ ํ‚ค ๊ฐ’ ์„ค์ •

    • setVersion() : ์ฟ ํ‚ค ๋ฒ„์ „ ์„ค์ •

    • getMaxAge() : ์ฟ ํ‚ค ๋งŒ๋ฃŒ๊ธฐ๊ฐ„ ์ •๋ณด๋ฅผ ์–ป์Œ

    • getName() : ์ฟ ํ‚ค ์ด๋ฆ„์„ ์–ป์Œ

    • getPath() : ์ฟ ํ‚ค ์‚ฌ์šฉ์˜ ์œ ํšจ ๋””๋ ‰ํ† ๋ฆฌ ์ •๋ณด๋ฅผ ์–ป์Œ

    • getValue() : ์ฟ ํ‚ค์˜ ๊ฐ’์„ ์–ป์Œ

    • getVersion() : ์ฟ ํ‚ค ๋ฒ„์ „์„ ์–ป์Œ



์ฟ ํ‚ค(Cookie)๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ„๋‹จํ•œ ๋กœ๊ทธ์ธ / ๋กœ๊ทธ์•„์›ƒ ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ


  • login.html

      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="UTF-8">
      <title>๋กœ๊ทธ์ธ</title>
      </head>
      <body>
            
          <form action="chkLogin.jsp" method="post">
              ์•„์ด๋”” : <input type="text" name="uid" size="10">
              <br><br>
              ๋น„๋ฐ€๋ฒˆํ˜ธ : <input type="password" name="upw" size="10">
              <br><br>
              <input type="submit" value="๋กœ๊ทธ์ธ">
              <input type="reset" value="์ดˆ๊ธฐํ™”">
          </form>
      </body>
      </html>
    
  • chkLogin.jsp

      <%@ page language="java" contentType="text/html; charset=UTF-8"
          pageEncoding="UTF-8"%>
      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="UTF-8">
      <title>๋กœ๊ทธ์ธ ํ™•์ธ ํŽ˜์ด์ง€</title>
      </head>
      <body>
          <%!
              String uid, upw;
          %>
          <%
              uid = request.getParameter("uid");
              upw = request.getParameter("upw");
            
              if(uid.equals("admin") && upw.equals("12345")){
                    
                  Cookie cookie = new Cookie("uid", uid);
                  cookie.setMaxAge(60);
                  response.addCookie(cookie);
                    
                  response.sendRedirect("loginSuccess.jsp");
                    
              }
              else response.sendRedirect("loginFail.jsp");
            
          %>
    
      </body>
      </html>
    
  • loginSuccess.jsp

      <%@ page language="java" contentType="text/html; charset=UTF-8"
          pageEncoding="UTF-8"%>
      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="UTF-8">
      <title>๋กœ๊ทธ์ธ ์„ฑ๊ณต</title>
      </head>
      <body>
          <h1>๋กœ๊ทธ์ธ ์„ฑ๊ณต</h1>
          <%
              Cookie[] cookies = request.getCookies();
            
              for(int i = 0; i < cookies.length; i++){
                    
                  String id = cookies[i].getValue();
                  if(id.equals("admin")){
                      out.println(id + "๋‹˜ ๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค.");
                  }
              }
          %>
            
          <a href="logout.jsp"><input type="button" value="๋กœ๊ทธ์•„์›ƒ"></a>
      </body>
      </html>
    
  • loginFail.ljsp

      <%@ page language="java" contentType="text/html; charset=UTF-8"
          pageEncoding="UTF-8"%>
      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="UTF-8">
      <title>๋กœ๊ทธ์ธ ์‹คํŒจ</title>
      </head>
      <body>
          <script type="text/javascript">
              alert('๋กœ๊ทธ์ธ ์‹คํŒจ');
              location.href="login.html";
          </script>
      </body>
      </html>
    
  • logout.jsp

      <%@ page language="java" contentType="text/html; charset=UTF-8"
          pageEncoding="UTF-8"%>
      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="UTF-8">
      <title>๋กœ๊ทธ์•„์›ƒ</title>
      </head>
      <body>
          <%
              Cookie[] cookies = request.getCookies();
                
              if(cookies != null){
                    
                  for(int i = 0; i < cookies.length; i++){
                      String id = cookies[i].getValue();
                        
                      if(id.equals("admin")){
                          //setMaxAge(0) == ์ฟ ํ‚ค ์‚ญ์ œ
                          cookies[i].setMaxAge(0);
                          response.addCookie(cookies[i]);
                          out.println("๋กœ๊ทธ์•„์›ƒ ํ•˜์…จ์Šต๋‹ˆ๋‹ค.");
                      }
                  }
              }
          %>
          <a href="login.html"><input type="button" value="๋‹ค์‹œ๋กœ๊ทธ์ธํ•˜๊ธฐ"></a>
      </body>
      </html>
    
  • ์‹คํ–‰๊ฒฐ๊ณผ

Categories:

JSP/Servlet