[JSP] ์ฟ ํ‚ค(Cookie)

์ด์ง€ํ˜„ยท2022๋…„ 4์›” 10์ผ
0
post-thumbnail

๐Ÿ“• 1. ์ฟ ํ‚ค(Cookie)๋ž€?

1-1. ์ฟ ํ‚ค(Cookie)๋ž€?

  • ํด๋ผ์ด์–ธํŠธ์˜ ์ƒํƒœ ์ •๋ณด๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ์ˆ ์ด๋‹ค.
  • ์ƒํƒœ ์ •๋ณด๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ํ‚ค์™€ ๊ฐ’ ํ˜•ํƒœ๋กœ ์ €์žฅํ–ˆ๋‹ค๊ฐ€ ๋‹ค์Œ ์š”์ฒญ์‹œ ์ €์žฅ๋œ ์ฟ ํ‚ค๋ฅผ ํ•จ๊ป˜ ์ „์†กํ•œ๋‹ค
    ๊ทธ๋Ÿฌ๋ฉด ์›น ์„œ๋ฒ„๋Š” ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ „์†กํ•œ ์ฟ ํ‚ค๋กœ๋ถ€ํ„ฐ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
_์ฟ ํ‚ค ํ‘œ์ค€์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ œ์•ฝ์ด ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. ์•„๋ž˜๋ฅผ ํ™•์ธํ•ด ๋ณด์ž!_
    1. 3000๊ฐœ๊นŒ์ง€ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
    1. ์ฟ ํ‚ค ํ•˜๋‚˜์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋Š” 4096 ๋ฐ”์ดํŠธ ์ด๋‹ค.
    1. ํ•˜๋‚˜์˜ ํ˜ธ์ŠคํŠธ๋‚˜ ๋„๋ฉ”์ธ์—์„œ ์ตœ๋Œ€ 50๊ฐœ๊นŒ์ง€ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“• 2. ์ฟ ํ‚ค์˜ ๋™์ž‘ ๊ตฌ์กฐ

    1. ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค.
    1. ์„œ๋ฒ„๊ฐ€ ์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ HTTP ์‘๋‹ต ํ—ค๋” ์— ์‹ค์–ด ํด๋ผ์ด์–ธํŠธ์— ์ „์†กํ•œ๋‹ค.
    1. ํด๋ผ์ด์–ธํŠธ๋Š” ์ฟ ํ‚ค๋ฅผ ๋ฐ›์•„ ์ €์žฅํ•ด๋‘”๋‹ค.
      (์ด ์‹œ์ ์—๋Š” ์ฟ ํ‚ค๊ฐ€ ํด๋ผ์ด์–ธํŠธ์—๋งŒ ์ €์žฅ๋œ ์ƒํƒœ์ด๋ฏ€๋กœ ์„œ๋ฒ„๋Š” ์•„์ง ์ฟ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.)
    1. ํด๋ผ์ด์–ธํŠธ๋Š” ๋‹ค์Œ๋ฒˆ ์š”์ฒญ ์‹œ ์ €์žฅํ•ด๋‘” ์ฟ ํ‚ค๋ฅผ HTTP ์š”์ฒญ ํ—ค๋”์— ์‹ค์–ด ๋ณด๋‚ธ๋‹ค.
    1. ์„œ๋ฒ„๋Š” ์ฟ ํ‚ค์˜ ์ •๋ณด๋ฅผ ์ฝ์–ด ํ•„์š”ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

๐Ÿ“• 3. ์ฟ ํ‚ค์˜ ๊ตฌ์„ฑ

์ด๋ฆ„์„ค๋ช…
์ด๋ฆ„(name)์ฟ ํ‚ค๋ฅผ ๊ตฌ๋ณ„ํ•˜๋Š” ์ด๋ฆ„
๊ฐ’(value)์ฟ ํ‚ค์— ์ €์žฅํ•  ์‹ค์ œ ๋ฐ์ดํ„ฐ
๋„๋ฉ”์ธ(domain)์ฟ ํ‚ค๋ฅผ ์ ์šฉํ•  ๋„๋ฉ”์ธ
๊ฒฝ๋กœ(path)์ฟ ํ‚ค๋ฅผ ์ ์šฉํ•  ๊ฒฝ๋กœ
์œ ์ง€ ๊ธฐ๊ฐ„(max age)์ฟ ํ‚ค๋ฅผ ์œ ์ง€ํ•  ๊ธฐ๊ฐ„

3-1. ์ฟ ํ‚ค์˜ ์ €์žฅAPI

์ด๋ฆ„์„ค๋ช…
void setValue(String value)- ์ฟ ํ‚ค์˜ ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค. ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜๋ฉด ๋˜๋Š”๋ฐ, ์‰ผํ‘œ๋‚˜ ์„ธ๋ฏธ์ฝœ๋ก  ๊ฐ™์€ ๋ฌธ์ž๋Š” ํฌํ•จํ•  ์ˆ˜ ์—†๋‹ค.

์ด๋ฆ„์„ค๋ช…
void setDomain(String domain)- ์ฟ ํ‚ค์— ์ ์šฉํ•  ๋„๋ฉ”์ธ์„ ์„ค์ •ํ•œ๋‹ค.
- ์ฃผ ๋„๋ฉ”์ธ๋งŒ ์ ์š”ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด "๋„๋ฉ”์ธ" ํ˜•ํƒœ๋กœ ๊ธฐ์ˆ ํ•œ๋‹ค.
- ์ฃผ ๋„๋ฉ”์ธ ์™ธ์— ์„œ๋ธŒ ๋„๋ฉ”์ธ์—๋„ ์ ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ".๋„๋ฉ”์ธ" ํ˜•ํƒœ๋กœ ๊ธฐ์ˆ ํ•œ๋‹ค.
(์˜ˆ์‹œ: setDomain(".nnnn.co.kr")๋กœ ์„ค์ •ํ•˜๋ฉด www.nnnn.co.kr์€ ๋ฌผ๋ก  mail.nnnn.co.kr์—๋„ ์ฟ ํ‚ค๊ฐ€ ์ ์šฉ๋œ๋‹ค.)

์ด๋ฆ„์„ค๋ช…
void setPath(String path)- ์ฟ ํ‚ค๊ฐ€ ์ ์šฉ๋  ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•œ๋‹ค. ์ง€์ •ํ•œ ๊ฒฝ๋กœ์™€ ๊ทธ ํ•˜์œ„ ๊ฒฝ๋กœ์—๊นŒ์ง€ ์ ์šฉ๋œ๋‹ค.

์ด๋ฆ„์„ค๋ช…
void setMaxAge(int expire_seconds)- ์ฟ ํ‚ค๊ฐ€ ์œ ์ง€๋  ๊ธฐ๊ฐ„์„ ์ดˆ ๋‹จ์œ„๋กœ ์„ค์ •ํ•œ๋‹ค.
- ๊ธฐ๊ฐ„์„ ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์›น ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋‹ซํž ๋•Œ ์ฟ ํ‚ค๋„ ๊ฐ™์ด ์‚ญ์ œ๋œ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์™œ ์ฟ ํ‚ค์˜ ์ด๋ฆ„ ์„ค์ •ํ•˜๋Š”๊ฒƒ์€ ์—†์„๊นŒ?

  • ๊ทธ ์ด์œ ๋Š” ์ฟ ํ‚ค์˜ ์ด๋ฆ„์€ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์„ค์ •ํ•˜๊ณ , ์ƒ์„ฑ ํ›„์—๋Š” ๋” ์ด์ƒ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ƒ์„ฑ์ž์˜ ํ˜•ํƒœ๋Š” ์•„๋ž˜๋ฅผ ํ†ตํ•ด ํ™•์ธํ•˜์ž!

์ด๋ฆ„์„ค๋ช…
์ฟ ํ‚ค์˜ ์ƒ์„ฑ์žnew Cookie(String name, String value)
์ด๋ฆ„๊ณผ ๊ฐ’์„ ๋ฐ›์•„ ์ƒˆ๋กœ์šด ์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

3-2. ์ฟ ํ‚ค์˜ ์ฝ๋Š”API

์ด๋ฆ„์„ค๋ช…
String getName()์ฟ ํ‚ค์˜ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
String getValue()์ฟ ํ‚ค์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
String getDomain()์ฟ ํ‚ค๊ฐ€ ์ ์šฉ๋˜๋Š” ๋„๋ฉ”์ธ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
String getPath()์ฟ ํ‚ค์˜ ์ ์šฉ ๊ฒฝ๋กœ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋‹จ, setPath()๋กœ ์„ค์ •ํ•œ ์ ์ด ์—†๋‹ค๋ฉด null์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
ubt getMaxAge()์ฟ ํ‚ค์˜ ์œ ์ง€ ๊ธฐ๊ฐ„์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋‹จ, setMaxAge()๋กœ ์„ค์ •ํ•œ ์ ์ด ์—†๋‹ค๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ“• 4. ์ฟ ํ‚ค์˜ ๊ธฐ๋ณธ ์กฐ์ž‘๋ฒ•

์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ธฐ๋ณธ ๋™์ž‘์„ ํ™œ์ธํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ๋ณด๋ฉด์„œ ๋ฐฐ์›Œ๋ณด์ž!

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<html>
<head><title>Cookie</title></head>
<body>
    <h2>1. ์ฟ ํ‚ค(Cookie) ์„ค์ •</h2>
    <%
	    Cookie cookie = new Cookie("myCookie", "์ฟ ํ‚ค๋ง›๋‚˜์š”");  // ์ฟ ํ‚ค ์ƒ์„ฑ
	    cookie.setPath(request.getContextPath());			// ๊ฒฝ๋กœ๋ฅผ ์ปจํ…์ŠคํŠธ ๋ฃจํŠธ๋กœ ์„ค์ •
	    cookie.setMaxAge(3600);  							// ์œ ์ง€ ๊ธฐ๊ฐ„์„ 1์‹œ๊ฐ„์œผ๋กœ ์„ค์ •
	    response.addCookie(cookie);  						// ์‘๋‹ต ํ—ค๋”์— ์ฟ ํ‚ค ์ถ”๊ฐ€
    %>

    <h2>2. ์ฟ ํ‚ค ์„ค์ • ์งํ›„ ์ฟ ํ‚ค๊ฐ’ ํ™•์ธํ•˜๊ธฐ</h2>
    <%
	    Cookie[] cookies = request.getCookies();  // ์š”์ฒญ ํ—ค๋”์˜ ๋ชจ๋“  ์ฟ ํ‚ค ์–ป๊ธฐ
	    if (cookies!=null) 
	    {
	        for (Cookie c : cookies) 
	        {  
	        	// ์ฟ ํ‚ค ๊ฐ๊ฐ์˜
	            String cookieName = c.getName();  	// ์ฟ ํ‚ค ์ด๋ฆ„ ์–ป๊ธฐ
	            String cookieValue = c.getValue();  // ์ฟ ํ‚ค ๊ฐ’ ์–ป๊ธฐ
	            
	            // ํ™”๋ฉด์— ์ถœ๋ ฅ
	            out.println(String.format("%s : %s<br/>", cookieName, cookieValue));
	        }
	    }
    %>

    <h2>3. ํŽ˜์ด์ง€ ์ด๋™ ํ›„ ์ฟ ํ‚ค๊ฐ’ ํ™•์ธํ•˜๊ธฐ</h2>
    <a href="CookieResult.jsp">
        ๋‹ค์Œ ํŽ˜์ด์ง€์—์„œ ์ฟ ํ‚ค๊ฐ’ ํ™•์ธํ•˜๊ธฐ
    </a>
</body>
</html>
  • ์ด๋ฆ„์ด "myCookie" ์ด๊ณ  ๊ฐ’์ด "์ฟ ํ‚ค ๋ง›๋‚˜์š”" ์ธ ์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑํ•œ ํ›„, ๊ฒฝ๋กœ์™€ ์œ ์ง€ ๊ธฐ๊ฐ„์„ ์„ค์ •ํ•ด ์‘๋‹ต ํ—ค๋”์— ์ถ”๊ฐ€ํ–ˆ๋‹ค.
  • cookie.setPath(request.getContextPath()); ์—์„œ ์„ค์ •ํ•œ ๊ฒฝ๋กœ๋Š” request ๋‚ด์žฅ ๊ฐ์ฒด ์˜ getContextPath() ๋กœ ์–ป์–ด์˜จ ์ปจํ…์ŠคํŠธ ๋ฃจํŠธ ์ด๋‹ค. ์ดํด๋ฆฝ์Šค์—์„œ๋Š” ํ”„๋กœ์ ํŠธ๋ช…์ด ์ปจํ…์ŠคํŠธ ๋ฃจํŠธ๋กœ ์‚ฌ์šฉ๋œ๋‹ค
    ์ฆ‰, ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์ฒด์—์„œ ์ฟ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.
  • 2. ์ฟ ํ‚ค ์„ค์ • ์งํ›„ ์ฟ ํ‚ค๊ฐ’ ํ™•์ธํ•˜๊ธฐ ๋ถ€๋ถ„์€ ์š”์ฒญ ํ—ค๋”์— ๋‹ด๊ฒจ์žˆ๋Š” ์ฟ ํ‚ค๋ฅผ ๋ชจ๋‘ ๊ฐ€์ ธ์™€์„œ ํ™•์žฅ for๋ฌธ ์„ ์ด์šฉํ•ด ์ด๋ฆ„ ๊ณผ ๊ฐ’ ์„ ํ™”๋ฉด์— ์ถœ๋ ฅํ•œ๋‹ค.
  • ์ฟ ํ‚ค๋ฅผ ์‚ญ์ œํ•  ๋•Œ๋Š” ์ฟ ํ‚ค ๋ฅผ ๋นˆ ๊ฐ’ ์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ์œ ์ง€ ๊ธฐ๊ฐ„์„ 0 ์œผ๋กœ ๋ถ€์—ฌํ•˜๋ฉด ๋œ๋‹ค.

๐Ÿ“• 5. ์ฟ ํ‚ค์˜ ์‚ฌ์šฉ ์˜ˆ์ œ (1)

์ฟ ํ‚ค๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž !

5-1. ๋ ˆ์ด์–ด ํŒ์—…์ฐฝ ์ œ์–ด

๐Ÿ“ PopupMain.jsp ๐Ÿ“
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%
	String popupMode = "on";
	
	Cookie[] cookies = request.getCookies();
	if (cookies != null) 
	{
	    for (Cookie c : cookies) 
	    {
	        String cookieName = c.getName();
	        String cookieValue = c.getValue();
	        
	        if (cookieName.equals("PopupClose")) 
	        {
	            popupMode = cookieValue; 
	        }
	    }
	} 
%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>์ฟ ํ‚ค๋ฅผ ์ด์šฉํ•œ ํŒ์—… ๊ด€๋ฆฌ</title>
	<style>
	    div#popup {
	        position: absolute; top:100px; left:100px; color:yellow;  
	        width:300px; height:100px; background-color:gray;
	    }
	    div#popup>div {
	        position: relative; background-color:#ffffff; top:0px;
	        border:1px solid gray; padding:10px; color:black;
	    }
	</style>	
</head>
<body>
<h2>ํŒ์—… ๋ฉ”์ธ ํŽ˜์ด์ง€</h2>
<%
    for (int i = 1; i <= 10; i++) 
    {
        out.print("ํ˜„์žฌ ํŒ์—…์ฐฝ์€ " + popupMode + " ์ƒํƒœ์ž…๋‹ˆ๋‹ค.<br/>");
    }

    if (popupMode.equals("on")) 
    {
%>
    <div id="popup">
        <h2 align="center">๊ณต์ง€์‚ฌํ•ญ ํŒ์—…์ž…๋‹ˆ๋‹ค.</h2>
        <div align="right">
	        <form name="popFrm">
	            <input type="checkbox" id="inactiveToday" value="1" />ํ•˜๋ฃจ ๋™์•ˆ ์—ด์ง€ ์•Š์Œ
	            <input type="button" value="๋‹ซ๊ธฐ" id="closeBtn" />
	        </form>
        </div>
    </div>
<%
    }
%>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script>
	$(function() 
	{
	    $('#closeBtn').click(function() 
	    {
	        $('#popup').hide();
	        const chkVal = $("input:checkbox[id=inactiveToday]:checked").val();
	        
	        $.ajax
	        ({
	            url : './PopupCookie.jsp',
	            type : 'get',
	            data : 
	            {
	            	inactiveToday : chkVal
	            },
	            dataType : "text",
	            success : function(resData) 
	            {
	                if (resData != '') 
	                {
	                	location.reload();
	                }
	            }
	        });
	    });
	});
</script>
</body>
</html>
๐Ÿ“ PopupCookie.jsp ๐Ÿ“
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>

<%
	String chkVal = request.getParameter("inactiveToday");
	
	if (chkVal != null && chkVal.equals("1")) 
	{
	    Cookie cookie = new Cookie("PopupClose", "off");	// ์ฟ ํ‚ค ์ƒ์„ฑ 
	    cookie.setPath(request.getContextPath());  			// ๊ฒฝ๋กœ ์„ค์ •
	    cookie.setMaxAge(60*60*24);							// ์œ ์ง€ ๊ธฐ๊ฐ„ ์„ค์ •
	    response.addCookie(cookie);							// ์‘๋‹ต ๊ฐ์ฒด์— ์ถ”๊ฐ€ 
	    
	    out.println("์ฟ ํ‚ค : ํ•˜๋ฃจ ๋™์•ˆ ์—ด์ง€ ์•Š์Œ");  
	}
%>
  • ์ฒดํฌ๋ฐ•์Šค ํผ๊ฐ’์„ ๋ฐ›๋Š”๋‹ค. [์˜ค๋Š˜ ํ•˜๋ฃจ ์—ด์ง€ ์•Š์Œ] ์ฒดํฌ๋ฐ•์Šค๋ฅผ ์ฒดํฌํ–ˆ๋‹ค๋ฉด ์ด ๊ฐ’์œผ๋กœ "1" ์ด ์ „๋‹ฌ๋œ๋‹ค.
    ๊ทธ๋Ÿฐ ๋‹ค์Œ ๊ฐ’์ด "1" ์ด๋ฉด ์ด๋ฆ„์ด "PopupClose" , ๊ฐ’์ด "off" ๋กœ ์ €์žฅ๋˜๊ณ 
    ๊ฒฝ๋กœ๋Š” ์ปจํ…์ŠคํŠธ ๋ฃจํŠธ, ์œ ์ง€ ๊ธฐ๊ฐ„์€ ํ•˜๋ฃจ ์ธ ์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑํ•ด ์‘๋‹ต ๊ฐ์ฒด์— ์ถ”๊ฐ€ ํ•œ๋‹ค.

๐Ÿ“• 6. ํฌ๋กฌ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ฟ ํ‚ค ์‚ญ์ œํ•˜๊ธฐ

  • ํฌ๋กฌ์˜ ๊ฒฝ์šฐ ์šฐ์ธก ์ƒ๋‹จ์˜ [๋” ๋ณด๊ธฐ] ๋ฉ”๋‰ด ์—์„œ [์„ค์ •] ์„ ํด๋ฆญํ•œ๋‹ค.

  • [๊ฐœ์ธ์ •๋ณด ๋ฐ ๋ณด์•ˆ] ์˜์—ญ์—์„œ [์ธํ„ฐ๋„ท ์‚ฌ์šฉ ๊ธฐ๋ก ์‚ญ์ œ] ๋ฅผ ํด๋ฆญํ•œ๋‹ค.

  • [๊ธฐ๋ณธ] ํƒญ๊ณผ [๊ณ ๊ธ‰] ํƒญ์— ๋ณด๋ฉด ๋‹ค์–‘ํ•œ ์˜ต์…˜์ด ์žˆ๋Š”๋ฐ, ๊ฐ€์žฅ ์ตœ๊ทผ์˜ ์ฟ ํ‚ค๋งŒ ์‚ญ์ œํ•˜๋ ค๋ฉด ๊ธฐ๊ฐ„์€ '์ง€๋‚œ 1์‹œ๊ฐ„' ์œผ๋กœ ์ œํ•œํ•˜๊ณ , '์ฟ ํ‚ค ๋ฐ ๊ธฐํƒ€ ์‚ฌ์ดํŠธ ๋ฐ์ดํ„ฐ' ํ•ญ๋ชฉ์„ ์ฒดํฌํ•œ ํ›„ [์ธํ„ฐ๋„ท ์‚ฌ์šฉ ๊ธฐ๋ก ์‚ญ์ œ] ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๋œ๋‹ค.


๐Ÿ“• 7. ์ฟ ํ‚ค์˜ ์‚ฌ์šฉ ์˜ˆ์ œ (2)

7-1 ๋กœ๊ทธ์ธ ์•„์ด๋”” ์ €์žฅ

์ฟ ํ‚ค๋ฅผ ์ด์šฉํ•œ ์•„์ด๋”” ์ €์žฅ ์‹œ๋‚˜๋ฆฌ์˜ค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค!

  • ๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ•œ ๊ฒฝ์šฐ์—๋งŒ ์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑ ๋ฐ ์‚ญ์ œํ•œ๋‹ค.
  • ์ฟ ํ‚ค์— ์ €์žฅ๋œ ์•„์ด๋””๊ฐ€ ์žˆ์œผ๋ฉด ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€์—์„œ๋Š” ์•„์ด๋””๊ฐ€ ์ž๋™ ์ž…๋ ฅ๋œ๋‹ค.
  • [์•„์ด๋”” ์ €์žฅํ•˜๊ธฐ] ์ฒดํฌ๋ฐ•์Šค๋ฅผ ํ•ด์ œํ•˜๊ณ  ๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ•˜๋ฉด ์ฟ ํ‚ค๊ฐ€ ์‚ญ์ œ๋œ๋‹ค

๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋ฅผ ๋ณด๊ธฐ ์ „์— JSP์—์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค๋ฅผ ๋จผ์ € ์ž‘์„ฑํ•˜๋„๋ก ํ•˜์ž!

์ด๋ฒˆ ์ ˆ์—๋Š” ๋ฉ”์‹œ์ง€ ์•Œ๋ฆผ์ฐฝ์„ ๋„์šด ํ›„ ๋‹ค์Œ ํŽ˜์ด์ง€๋‚˜ ์ด์ „ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๊ฐ€ ํ•„์š”ํ•˜๋‹ค

script ์ฝ”๋“œ๋ฅผ ์“ฐ๊ฒŒ๋˜๋ฉด ์†Œ์Šค๊ฐ€ ์ง€์ €๋ถ„ํ•ด์ง€๊ณ  ๋ฐ˜๋ณต๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ๋งŽ์•„์ง€๊ธฐ์— ๋ณ„๋„์˜ ๋„์šฐ๋ฏธ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋†“์ž!

๐Ÿ“ ๋ณ„๋„์˜ ๋„์šฐ๋ฏธ ํด๋ž˜์Šค: JSFunction.java ๐Ÿ“
package utils;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

public class JSFunction {
	
    // ๋ฉ”์‹œ์ง€ ์•Œ๋ฆผ์ฐฝ์„ ๋„์šด ํ›„ ๋ช…์‹œํ•œ URL๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
    public static void alertLocation(String msg, String url, JspWriter out) 
    {  	
        try 
        {
            String script = ""  // ์‚ฝ์ž…ํ•  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ
                          + "<script>"
                          + "    alert('" + msg + "');"
                          + "    location.href='" + url + "';"
                          + "</script>";
            out.println(script);  // ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ out ๋‚ด์žฅ ๊ฐ์ฒด๋กœ ์ถœ๋ ฅ(์‚ฝ์ž…)
        }
        catch (Exception e) {}
    }

    // ๋ฉ”์‹œ์ง€ ์•Œ๋ฆผ์ฐฝ์„ ๋„์šด ํ›„ ์ด์ „ ํŽ˜์ด์ง€๋กœ ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค.
    public static void alertBack(String msg, JspWriter out) 
    {
        try 
        {
            String script = ""
                          + "<script>"
                          + "    alert('" + msg + "');"
                          + "    history.back();"
                          + "</script>";
            out.println(script);
        }
        catch (Exception e) {}
    }

    // ๋ฉ”์‹œ์ง€ ์•Œ๋ฆผ์ฐฝ์„ ๋„์šด ํ›„ ๋ช…์‹œํ•œ URL๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
    public static void alertLocation(HttpServletResponse resp, String msg, String url) 
    {
        try 
        {
            resp.setContentType("text/html;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            String script = ""
                          + "<script>"
                          + "    alert('" + msg + "');"
                          + "    location.href='" + url + "';"
                          + "</script>";
            writer.print(script);
        }
        catch (Exception e) {}
    }

    // ๋ฉ”์‹œ์ง€ ์•Œ๋ฆผ์ฐฝ์„ ๋„์šด ํ›„ ์ด์ „ ํŽ˜์ด์ง€๋กœ ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค.
    public static void alertBack(HttpServletResponse resp, String msg) {
        try {
            resp.setContentType("text/html;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            String script = ""
                          + "<script>"
                          + "    alert('" + msg + "');"
                          + "    history.back();"
                          + "</script>";
            writer.print(script);
        }
        catch (Exception e) {}
    }
}
  • msg : ์•Œ๋ฆผ์ฐฝ์—์„œ ๋„์šธ ๋ฉ”์‹œ์ง€
  • url : ์•Œ๋ฆผ์ฐฝ์„ ๋‹ซ์€ ํ›„ ์ด๋™ํ•  ํŽ˜์ด์ง€์˜ URL
  • out : .์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•  ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ(JSP์˜ out ๋‚ด์žฅ ๊ฐ์ฒด)

์ด์ œ ์ฟ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ์ฟ ํ‚ค๋„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์–ด๋ณด์ž !

๐Ÿ“ CookieManager.java ๐Ÿ“
package utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieManager 
{
    // ๋ช…์‹œํ•œ ์ด๋ฆ„, ๊ฐ’, ์œ ์ง€ ๊ธฐ๊ฐ„ ์กฐ๊ฑด์œผ๋กœ ์ƒˆ๋กœ์šด ์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
    public static void makeCookie(HttpServletResponse response, String cName, String cValue, int cTime) 
    {
        Cookie cookie = new Cookie(cName, cValue);	// ์ฟ ํ‚ค ์ƒ์„ฑ
        cookie.setPath("/");         				// ๊ฒฝ๋กœ ์„ค์ •
        cookie.setMaxAge(cTime);     				// ์œ ์ง€ ๊ธฐ๊ฐ„ ์„ค์ •
        
        response.addCookie(cookie);  				// ์‘๋‹ต ๊ฐ์ฒด์— ์ถ”๊ฐ€
    }

    // ๋ช…์‹œํ•œ ์ด๋ฆ„์˜ ์ฟ ํ‚ค๋ฅผ ์ฐพ์•„ ๊ทธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    public static String readCookie(HttpServletRequest request, String cName) 
    {
        String cookieValue = "";  // ๋ฐ˜ํ™˜ ๊ฐ’

        Cookie[] cookies = request.getCookies();
        if (cookies != null) 
        {
            for (Cookie c : cookies) 
            {
                String cookieName = c.getName();
                if (cookieName.equals(cName)) 
                {
                    cookieValue = c.getValue();  // ๋ฐ˜ํ™˜ ๊ฐ’ ๊ฐฑ์‹ 
                }
            }
        }
        return cookieValue;
    }

    // ๋ช…์‹œํ•œ ์ด๋ฆ„์˜ ์ฟ ํ‚ค๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
    public static void deleteCookie(HttpServletResponse response, String cName) 
    {
        makeCookie(response, cName, "", 0);
    }
    
}//////////////// class

์ฟ ํ‚ค ํด๋ž˜์Šค๊นŒ์ง€ ๋‹ค ๋งŒ๋“ค์—ˆ์œผ๋‹ˆ ๋ณธ๊ฒฉ์ ์œผ๋กœ ์ฟ ํ‚ค๋ฅผ ํ†ตํ•ด ๋กœ๊ทธ์ธ ์•„์ด๋””๋ฅผ ์ €์žฅํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค์–ด๋ณด์ž!

๐Ÿ“ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ : IdSaveMain.jsp ๐Ÿ“
<%@ page import="utils.CookieManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%
	String loginId = CookieManager.readCookie(request, "loginId");
	
	String cookieCheck = "";
	if (!loginId.equals("")) 
	{
	    cookieCheck = "checked";
	}
%>

<html>
<head><title>Cookie - ์•„์ด๋”” ์ €์žฅํ•˜๊ธฐ</title></head>
<body>
	<h2>๋กœ๊ทธ์ธ ํŽ˜์ด์ง€</h2>
	<form action="IdSaveProcess.jsp" method="post">
		์•„์ด๋”” :	<input type="text" name="user_id" value="<%= loginId %>" />  
        		<input type="checkbox" name="save_check" value="Y" <%= cookieCheck %> />์•„์ด๋”” ์ €์žฅํ•˜๊ธฐ <br />
    	ํŒจ์Šค์›Œ๋“œ : <input type="text" name="user_pw" /><br />
    			<input type="submit" value="๋กœ๊ทธ์ธํ•˜๊ธฐ" />
    </form>
</body>
</html>
๐Ÿ“ IdSaveProcess.jsp ๐Ÿ“
<%@ page import="utils.CookieManager"%>
<%@ page import="utils.JSFunction"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  

<%
	String user_id = request.getParameter("user_id"); 
	String user_pw = request.getParameter("user_pw");
	String save_check = request.getParameter("save_check");  
	
	if ("must".equals(user_id) && "1234".equals(user_pw)) 
	{ 
	    // ๋กœ๊ทธ์ธ ์„ฑ๊ณต
	    if (save_check != null && save_check.equals("Y")) 
	    { 
	        CookieManager.makeCookie(response, "loginId", user_id, 86400); 
	    } 
	    else 
	    {
	        CookieManager.deleteCookie(response, "loginId"); 
	    }
	    
	    JSFunction.alertLocation("๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค.", "IdSaveMain.jsp", out); 
	}
	else 
	{
	    // ๋กœ๊ทธ์ธ ์‹คํŒจ
	    JSFunction.alertBack("๋กœ๊ทธ์ธ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.", out);  
	}
%>
  • ์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ ํ›„ ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ฒŒ ๋˜๋ฉด IdSaveProcess.jsp ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๊ฒŒ ๋œ๋‹ค.
  • ์œ„ ์˜ˆ์ œ์—์„œ๋Š” ํŽธ์˜ ์ƒ ์•„์ด๋””๋Š” "must" ๋กœ ํ•˜๊ณ , ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” "1234" ๋ผ๊ณ  ์ง€์ •ํ•˜์˜€๋‹ค.

๐Ÿ“• 8. ํ•ต์‹ฌ ์š”์•ฝ

  • ์ฟ ํ‚ค๋Š” ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด์„œ๋งŒ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
    ์ƒ์„ฑ ํ›„ ์ฟ ํ‚ค๊ฐ’์€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ ์ฟ ํ‚ค๋ช…์€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
  • setPath() ๋ฉ”์„œ๋“œ๋กœ ์ ์šฉํ•  ๊ฒฝ๋กœ๋ฅผ ์„ค์ •ํ•œ๋‹ค.
  • setMaxAge() ๋ฉ”์„œ๋“œ๋กœ ์ฟ ํ‚ค์˜ ์œ ์ง€ ๊ธฐ๊ฐ„์„ ์„ค์ •ํ•œ๋‹ค.
  • response ๋‚ด์žฅ ๊ฐ์ฒด์˜ addCookie() ๋ฉ”์„œ๋“œ๋กœ ํด๋ผ์ด์–ธํŠธ์— ์ฟ ํ‚ค๋ฅผ ์ €์žฅํ•œ๋‹ค.
  • ์ฟ ํ‚ค๋Š” ์ƒ์„ฑ ์งํ›„ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์—†๋‹ค. ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์žฌ์š”์ฒญ์„ ํ–ˆ์„ ๋•Œ๋ถ€ํ„ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
profile
๊ฐœ๋ฐœ ๊ณต๋ถ€ ์ค‘์ธ ์ง€ํ˜„์ด

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