Cookie And Session

์ฟ ํ‚ค๋ž€?

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

์—ฐ๊ฒฐ์ด ๋Š๊ฒผ์„ ๋•Œ ์–ด๋–ค ์ •๋ณด๋ฅผ ์ง€์†์ ์œผ๋กœ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ์ˆ˜๋‹จ์œผ๋กœ ์ฟ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ฟ ํ‚ค๋Š” ์„œ๋ฒ„์—์„œ ์ƒ์„ฑํ•˜๋ฉฐ, ์„œ๋ฒ„๊ฐ€ ์•„๋‹Œ ํด๋ผ์ด์–ธํŠธ์ธก์— ํŠน์ • ์ •๋ณด๋ฅผ ์ €์žฅํ•œ๋‹ค. ์„œ๋ฒ„์— ์š”์ฒญํ•  ๋•Œ ๋งˆ๋‹ค ์ฟ ํ‚ค์˜ ์†์„ฑ๊ฐ’์„ ์ฐธ์กฐ ๋˜๋Š” ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

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

๋ฉ”์†Œ๋“œ

์„ค๋ช…

setMaxAge()

์ฟ ํ‚ค ์œ ํšจ๊ธฐ๊ฐ„์„ ์„ค์ • ํ•ฉ๋‹ˆ๋‹ค.

setpath()

์ฟ ํ‚ค์‚ฌ์šฉ์˜ ์œ ํšจ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์„ค์ • ํ•ฉ๋‹ˆ๋‹ค.

setValue()

์ฟ ํ‚ค์˜ ๊ฐ’์„ ์„ค์ • ํ•ฉ๋‹ˆ๋‹ค.

setVersion()

์ฟ ํ‚ค ๋ฒ„์ „์„ ์„ค์ • ํ•ฉ๋‹ˆ๋‹ค.

getMaxAge()

์ฟ ํ‚ค ์œ ํšจ๊ธฐ๊ฐ„ ์ •๋ณด๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค.

getName()

์ฟ ํ‚ค ์ด๋ฆ„์„ ์–ป์Šต๋‹ˆ๋‹ค.

getPath()

์ฟ ํ‚ค์‚ฌ์šฉ์˜ ์œ ํšจ ๋””๋ ‰ํ† ๋ฆฌ ์ •๋ณด๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค.

getValue()

์ฟ ํ‚ค์˜ ๊ฐ’์„ ์–ป์Šต๋‹ˆ๋‹ค.

getVersion()

์ฟ ํ‚ค ๋ฒ„์ „์„ ์–ป์Šต๋‹ˆ๋‹ค.

์ฟ ํ‚ค ์ƒ์„ฑ(์„œ๋ฒ„) โ†’ ์†์„ฑ ์„ค์ • โ†’response๊ฐ์ฒด์— ํƒ‘์žฌ ์ˆœ์„œ๋กœ ์ง„ํ–‰๋œ๋‹ค.

    <% 
        Cookie cookie = new Cookie("CookieName","CookieValue");
        /* ์–ผ๋งˆ๋™์•ˆ ์œ ์ง€๋  ๊ฒƒ์ธ์ง€ ์ตœ๋Œ€ ์ˆ˜๋ช…์ด๋‹ค. */
        cookie.setMaxAge(60*60);
        response.addCookie(cookie);
    %>
    <%
        Cookie[] cookies = request.getCookies();

        for(int i=0;i<cookies.length;i++){
            out.println("cookies[" + i + "] name : " + cookies[i].getName() + "<br />");
            out.println("cookies[" + i + "] value : " + cookies[i].getValue() + "<br />");
            out.println("=====================<br />");
        }
    %>

์ฟ ํ‚ค ์‚ญ์ œ๋Š” ์œ ํšจ๊ธฐ๊ฐ„ setMaxAge๋ฅผ 0์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ์†์„ฑ ๋ณ€๊ฒฝํ•œ ๊ฒƒ์„ response ๊ฐ์ฒด์— ์ ์šฉํ•˜๋ฉด๋œ๋‹ค.

    <%
        Cookie[] cookies = request.getCookies();
        for(int i=0;i<cookies.length;i++){
            out.println("cookie name : "+cookies[i].getName() );
            cookies[i].setMaxAge(0);
            response.addCookie(cookies[i]);
        }
    %>
  • ์ฟ ํ‚ค ์‚ฌ์šฉ ์˜ˆ์ œ

    • ์ž๋™ ๋กœ๊ทธ์ธ

    • ํŒ์—… ex)"์˜ค๋Š˜ ๋” ์ด์ƒ ์ด ์ฐฝ์„ ๋ณด์ง€ ์•Š์Œ"

    • ์‡ผํ•‘๋ชฐ์˜ ์žฅ๋ฐ”๊ตฌ๋‹ˆ

์ฟ ํ‚ค๋Š” ๋ณด์•ˆ์ƒ์˜ ๋ฌธ์ œ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ํ•ดํ‚น๊ณผ ๊ฐ™์€ ํ•˜์ง€๋งŒ ๊ผญ ์•Œ๊ณ  ์žˆ์–ด์•ผํ•˜๋Š” ๊ฐœ๋…์ด๋‹ค.

์„ธ์…˜

์„ธ์…˜์ด๋ž€?

์„ธ์…˜๋„ ์ฟ ํ‚ค์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์„œ๋ฒ„์™€์˜ ๊ด€๊ณ„๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ์ˆ˜๋‹จ์ด๋‹ค.

๋‹จ, ์ฟ ํ‚ค์™€ ๋‹ฌ๋ฆฌ ํด๋ผ์ด์–ธํŠธ์˜ ํŠน์ • ์œ„์น˜์— ์ €์žฅ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์„œ๋ฒ„์ƒ์— ๊ฐ์ฒด๋กœ ์กด์žฌํ•œ๋‹ค. (์ฟ ํ‚ค๋Š” ๋กœ์ปฌ์ƒ์— ์ €์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณด์•ˆ์— ์ทจ์•ฝํ•˜๋‹ค.)๋”ฐ๋ผ์„œ ์„ธ์…˜์€ ์„œ๋ฒ„์—์„œ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜์—ฌ ๋ณด์•ˆ์ด ์ข‹๊ณ , ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ์— ํ•œ๊ณ„๊ฐ€ ์—†๋‹ค.

๋กœ๊ทธ์ธ๊ณผ ๊ฐ™์ด ์ •๋ณด๋ฅผ ์ €์žฅํ•  ๋•Œ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.

๋ฌธ๋ฒ•

์„ธ์…˜์€ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์ด ๋ฐœ์ƒํ•˜๋ฉด ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค. session์ด๋ผ๋Š” ๋‚ด๋ถ€ ๊ฐ์ฒด๋ฅผ ์ง€์›ํ•˜์—ฌ ์„ธ์…˜์˜ ์†์„ฑ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ด€๋ จ ๋ฉ”์†Œ๋“œ

์„ค๋ช…

setAttribute()

์„ธ์…˜์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ ํ•ฉ๋‹ˆ๋‹ค.

getAttribute()

์„ธ์…˜์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค.

getAttributeNames()

์„ธ์…˜์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ์˜ ์ด๋ฆ„(์œ ๋‹ˆํฌํ•œ ํ‚ค๊ฐ’)์„ ์–ป์Šต๋‹ˆ๋‹ค.

getId()

์ž๋™ ์ƒ์„ฑ๋œ ์„ธ์…˜์˜ ์œ ๋‹ˆํฌํ•œ ์•„์ด๋””๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค.

isNew()

์„ธ์…˜์ด ์ตœ์ดˆ ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€, ์ด์ „์— ์ƒ์„ฑ๋œ ์„ธ์…˜์ธ์ง€๋ฅผ ๊ตฌ๋ถ„ ํ•ฉ๋‹ˆ๋‹ค.

getMaxInactiveInterval()

์„ธ์…˜์˜ ์œ ํšจ์‹œ๊ฐ„์„ ์–ป์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ตœ๊ทผ ์š”์ฒญ์‹œ์ ์„ ๊ธฐ์ค€์œผ๋กœ ์นด์šดํŠธ๋ฉ๋‹ˆ๋‹ค. (C:\javalec\apache-tomcat-7.0.57\apache-tomcat-7.0.57\conf\web.xml ์ฐธ์กฐ)

removeAttribute()

์„ธ์…˜์—์„œ ํŠน์ • ๋ฐ์ดํ„ฐ ์ œ๊ฑฐ ํ•ฉ๋‹ˆ๋‹ค.

Invalidate()

์„ธ์…˜์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œ ํ•ฉ๋‹ˆ๋‹ค.

session.getAttribute()๋Š” Objectํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜๋œ๋‹ค. ๋ณ€ํ™˜์ด ํ•„์š”ํ•˜๋‹ค.

Init

    <%
        session.setAttribute("sessionName", "sessionValue");
        session.setAttribute("abcde", 12345);
    %>

Get

    <%
        Object obj1 = session.getAttribute("mySessionName");
        String mySessionName = (String)obj1;
        out.println(mySessionName +"<br />");
    %>

์„ธ์…˜์€ ๋กœ๊ทธ์ธ ์ •๋ณด๋ฅผ ์œ ์ง€ํ•˜๋Š”๋ฐ ์žˆ์–ด์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.

์ฐจ์ด์ 

Session

Cookie

์ €์žฅ์œ„์น˜

์„œ๋ฒ„์— ์ €์žฅ

ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ

๋ณด์•ˆ

๋ณด์•ˆ ์ˆ˜์ค€ ๋†’์Œ

๋ณด์•ˆ ์ทจ์•ฝ ํด๋ผ์ด์–ธํŠธ ๋กœ์ปฌ์— ์ €์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ

๋ผ์ดํ”„ ์‚ฌ์ดํด

์„ธ์…˜๋„ ๋งŒ๋ฃŒ์‹œ์ ์„ ์ •ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ธŒ๋ผ์šฐ์ € ์ข…๋ฃŒ์‹œ ์‚ญ์ œ

์ง€์ •ํ•œ ์ฟ ํ‚ค ๋งŒ๋ฃŒ์‹œ๊ฐ„

์„ธ์…˜์€ ์„œ๋ฒ„์˜ ์ž์›์„ ์‚ฌ์šฉํ•˜๊ธฐ๋•Œ๋ฌธ์— ๋ฌด๋ถ„๋ณ„ํ•˜๊ฒŒ ๋งŒ๋“ค๋‹ค๋ณด๋ฉด ์„œ๋ฒ„์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๊ฐ๋‹นํ•  ์ˆ˜ ์—†์–ด์งˆ ์ˆ˜๊ฐ€ ์žˆ๊ณ  ์†๋„๊ฐ€ ๋А๋ ค์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์˜ํ•ด์•ผํ•œ๋‹ค.

์˜ˆ์ œ

์ฟ ํ‚ค

<!--login.html-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="logincheck.jsp" method="post">
        ์•„์ด๋”” : <input type="text" name="id" size="10"><br>
        ๋น„๋ฐ€๋ฒˆํ˜ธ : <input type="password" name="pw" size="10">
        <input type="submit" value="์ œ์ถœ">
    </form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%!
        String id,pw; 
    %>
    <%
        id = request.getParameter("id");
        pw = request.getParameter("pw");

        if(id.equals("abcde")&&pw.equals("12345")){
            Cookie cookie = new Cookie("id",id);
            cookie.setMaxAge(60*60);
            response.addCookie(cookie);
            response.sendRedirect("welcome.jsp");
        }else{
            response.sendRedirect("login.html");
        }

    %>

</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        Cookie[] cookies = request.getCookies();

        if(cookies!=null){
            for(int i=0;i<cookies.length;i++){
                if(cookies[i].getValue().equals("abcde")){
                    cookies[i].setMaxAge(0);
                    response.addCookie(cookies[i]);
                }
            }    
        }

        response.sendRedirect("cookietest.jsp");

    %>

</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        Cookie[] cookies = request.getCookies();

        for(int i=0;i<cookies.length;i++){
            String id = cookies[i].getValue();
            if(id.equals("abcde"))
                out.println(id  + "๋‹˜ ์•ˆ๋…•ํ•˜์„ธ์š”!");
        }

    %>

    <a href="logout.jsp">๋กœ๊ทธ์•„์›ƒํ•˜๊ธฐ</a>
</body>
</html>

์„ธ์…˜

๋กœ๊ทธ์ธ

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="logincheck2.jsp" method="post">
        ์•„์ด๋”” : <input type="text" name="id" size="10"><br>
        ๋น„๋ฐ€๋ฒˆํ˜ธ : <input type="password" name="pw" size="10">
        <input type="submit" value="์ œ์ถœ">
    </form>
</body>
</html>

๋กœ๊ทธ์ธํ™•์ธ

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%!
        String id,pw; 
    %>
    <%
        id = request.getParameter("id");
        pw = request.getParameter("pw");

        if(id.equals("abcde")&&pw.equals("12345")){
            session.setAttribute("id", id);
            response.sendRedirect("welcome2.jsp");
        }else{
            response.sendRedirect("login.html");
        }

    %>


</body>
</html>

๋กœ๊ทธ์ธ ํ›„ ํ™”๋ฉด

<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        String sName, sValue;
        Enumeration enumeration = session.getAttributeNames();

        while(enumeration.hasMoreElements()){
            sName = enumeration.nextElement().toString();
            sValue = session.getAttribute(sName).toString();
            out.println( sValue + "๋‹˜ ์•ˆ๋…•ํ•˜์„ธ์š”!<br />");
        }

    %>

    <a href="logout2.jsp">๋กœ๊ทธ์•„์›ƒํ•˜๊ธฐ</a>
</body>
</html>

๋กœ๊ทธ์•„์›ƒ(์„ธ์…˜ ์‚ญ์ œ)

<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>

    <%
        Enumeration enumeration = session.getAttributeNames();
        while(enumeration.hasMoreElements()) {
            String sName = enumeration.nextElement().toString();
            String sValue = (String)session.getAttribute(sName);

            if(sValue.equals("abcde")) session.removeAttribute(sName);
        }

    %>

    <a href="sessiontest.jsp">sessionTest</a>

</body>
</html>

์ฐธ๊ณ ํŽ˜์ด์ง€

Last updated

Was this helpful?