Cookies ๋Š” ๊ทธ๋ƒฅ ์ผ๋ฐ˜ text files ์ด์ฃ . ์•„๋งˆ JSP๋ฅผ ๊ณต๋ถ€ํ•˜์‹œ๋Š” ๋ถ„๋“ค์ด๋ผ๋ฉด ์ด์ •๋„๋Š” ์•Œ๊ณ  ๊ณ„์‹œ๊ฒ ์ฃ ? 

JSP๋กœ HTTP cookies ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ํ•œ๋ฒˆ ์•Œ์•„๋ณผํ…๋ฐ ๊ทธ ์ „์— ํ•˜๋‚˜๋งŒ ์งš๊ณ  ๋„˜์–ด๊ฐˆ๊นŒ์š”?

์‚ฌ์šฉ์ž๊ฐ€ ํŠน์ • ์‚ฌ์ดํŠธ์— ์žฌ์ ‘์† ํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ์›น์‚ฌ์ดํŠธ์—์„œ ์–ด๋–ป๊ฒŒ ์•Œ๊นŒ์š”?

3์ดˆ๋งŒ์— ๋‹ต์ด ์•ˆ๋‚˜์˜จ๋‹ค๋ฉด ์•„๋ž˜๋ฅผ ์ฝ์–ด๋ด…์‹œ๋‹ค.

 

์žฌ์ ‘์†ํ•˜๋Š” ์‚ฌ์šฉ์ž๋ฅผ ํŒ๋‹จํ•˜๋Š” ๊ณผ์ •์€ ์„ธ ๋‹จ๊ณ„๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค.

  • ์„œ๋ฒ„์ชฝ์—์„œ ์ฟ ํ‚ค๋ฅผ ๋ธŒ๋ผ์šฐ์ €๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค.  

  • Browser๋Š” ์„œ๋ฒ„์—์„œ ๋ณด๋‚ธ ์ฟ ํ‚ค๋ฅผ ๋กœ์ปฌ์— ์ €์žฅ์„ ํ•˜๊ฒ ์ฃ .

  • ์ด์ œ ์‚ฌ์šฉ์ž๊ฐ€ ์ด ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ด์šฉํ•ด์„œ ํŠน์ •์‚ฌ์ดํŠธ์— ์ ‘์†ํ•  ๋•Œ ์ €์žฅ๋˜์–ด์žˆ๋Š” ์ฟ ํ‚ค์˜ ์ •๋ณด๊ฐ€ ์„œ๋ฒ„์ชฝ์œผ๋กœ ์ „๋‹ฌ๋˜๊ณ  ์„œ๋ฒ„์ชฝ์—์„œ๋Š” ์ด ์ •๋ณด๋ฅผ ์ด์šฉํ•ด์„œ ์‚ฌ์šฉ์ž๋ฅผ ํŒ๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์ฃ .  

์ฟ ํ‚ค๋Š” ๋‹ค๋ฅธ ์ •๋ณด๋กœ๋„ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ฟ ํ‚ค๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฒ•์„ ํ•œ๋ฒˆ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ฟ ํ‚ค ์„ธํŒ…, ๋ฆฌ์…‹, ์ฝ๊ธฐ, ์‚ญ์ œํ•˜๊ธฐ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.  


Cookie ํ•ด๋ถ€ํ•˜๊ธฐ

 ์ฟ ํ‚ค๋Š” ๋ณดํ†ต HTTP header ์•ˆ์— ์„ธํŒ…๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ( JavaScript ๋Š” ์ง์ ‘ ๋ธŒ๋ผ์šฐ์ €์— ์ฟ ํ‚ค๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค๋„ค์š” ). ์ฟ ํ‚ค๋ฅผ ์„ธํŒ…ํ•˜๋Š” JSP ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํ—ค๋”๋ฅผ ๋ณด๋‚ผ๊ฑฐ์—์š”. 

HTTP/1.1 200 OK Date: Fri, 04 Feb 2000 21:03:38 GMT Server: Apache/1.3.9 (UNIX) PHP/4.0b3 Set-Cookie: name=xyz; expires=Friday, 04-Feb-07 22:03:38 GMT; path=/; domain=tutorialspoint.com Connection: close Content-Type: text/html

์ด๋ฏธ request ๊ฐ์ฒด์‹œ๊ฐ„์— ํ•œ๋ฒˆ ๋ณธ์ ์ด ์žˆ๋˜ ๋‚ด์šฉ์ด์ฃ ? ๊ทธ๋Ÿฐ๋ฐ ์ถ”๊ฐ€๋œ๊ฒŒ ํ•˜๋‚˜ ์žˆ๋„ค์š”. Set-Cookie header ์ž…๋‹ˆ๋‹ค. ๋ณด์•„ํ•˜๋‹ˆ ์ด๋ฆ„, ๋งŒ๋ฃŒ์ผ์‹œ, ๊ฒฝ๋กœ, ๊ทธ๋ฆฌ๊ณ  ๋„๋ฉ”์ธ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋„ค์š”.  

๋ธŒ๋ผ์šฐ์ €์—์„œ ์ฟ ํ‚ค๋ฅผ ์ €์žฅํ•˜๋„๋ก ์„ค์ •์ด ๋˜์–ด์žˆ์œผ๋ฉด ๋ธŒ๋ผ์šฐ์ €๋Š” ์ด๋Ÿฐ ์ •๋ณด๋“ค์„ ๋งŒ๋ฃŒ์ผ ์ „๊นŒ์ง€ ์ €์žฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.๋งŒ์•ฝ ์‚ฌ์šฉ์ž๊ฐ€ ์ฟ ํ‚ค์— ์„ค์ •๋œ ๊ฒฝ๋กœ๋กœ ์ ‘์†์„ ํ•˜๊ฒŒ๋˜๋ฉด ๋ธŒ๋ผ์šฐ์ €๋Š” ์„œ๋ฒ„๋กœ ๊ทธ ์ฟ ํ‚ค๋ฅผ ์žฌ์ „์†กํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๋ธŒ๋ผ์šฐ์ €์˜ ํ—ค๋”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ •๋ณด๋ฅผ ๊ฐ–๊ณ  ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค. 

GET / HTTP/1.0 Connection: Keep-Alive User-Agent: Mozilla/4.6 (X11; I; Linux 2.2.6-15apmac ppc) Host: zink.demon.co.uk:1126 Accept: image/gif, */* Accept-Encoding: gzip Accept-Language: en Accept-Charset: iso-8859-1,*,utf-8 Cookie: name=xyz

์ด์ œ JSP script ๋Š” request method์ค‘ ํ•˜๋‚˜์ธ request.getCookies() ๋ฅผ ํ†ตํ•ด์„œ ์ฟ ํ‚ค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

Servlet Cookies ๋ฉ”์†Œ๋“œ

์•„๋ž˜ ๋ฉ”์†Œ๋“œ๋“ค์€ JSP์—์„œ ์ฟ ํ‚ค๋ฅผ ๋‹ค๋ฃฐ๋•Œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋ชฉ๋ก ๋ฐ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.

 

S.N.Method & Description
1public void setDomain(String pattern)
This method sets the domain to which cookie applies, for example tutorialspoint.com.
2public String getDomain()
This method gets the domain to which cookie applies, for example tutorialspoint.com.
3public void setMaxAge(int expiry)
This method sets how much time (in seconds) should elapse before the cookie expires. If you don't set this, the cookie will last only for the current session.
4public int getMaxAge()
This method returns the maximum age of the cookie, specified in seconds, By default, -1 indicating the cookie will persist until browser shutdown.
5public String getName()
This method returns the name of the cookie. The name cannot be changed after creation.
6public void setValue(String newValue)
This method sets the value associated with the cookie.
7public String getValue()
This method gets the value associated with the cookie.
8public void setPath(String uri)
This method sets the path to which this cookie applies. If you don't specify a path, the cookie is returned for all URLs in the same directory as the current page as well as all subdirectories.
9public String getPath()
This method gets the path to which this cookie applies.
10public void setSecure(boolean flag)
This method sets the boolean value indicating whether the cookie should only be sent over encrypted (i.e. SSL) connections.
11public void setComment(String purpose)
This method specifies a comment that describes a cookie's purpose. The comment is useful if the browser presents the cookie to the user.
12public String getComment()
This method returns the comment describing the purpose of this cookie, or null if the cookie has no comment.


 JSP๋กœ ์ฟ ํ‚ค ์„ค์ •ํ•˜๊ธฐ

JSP๋กœ ์ฟ ํ‚ค๋ฅผ ์„ค์ •ํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์นฉ๋‹ˆ๋‹ค.

 

(1) Cookie ๊ฐ์ฒด ์ƒ์„ฑ : You call the Cookie constructor with a cookie name and a cookie value, both of which are strings.

Cookie cookie = new Cookie("key","value");

์ž๋ฐ”์—์„œ ์ฟ ํ‚ค ์ƒ์„ฑํ•˜๋Š”๊ฑฐ๋ž‘ ๋˜‘๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ name๊ณผ value ( "key"๊ฐ€ name์ธ๊ฑฐ๊ณ  "value"๊ฐ€ value๊ฐ€ ๋˜๊ฒ ์ฃ )์—๋Š” ์ ˆ๋Œ€๋กœ ์ŠคํŽ˜์ด์Šค(๋นˆ์นธ)์ด๋‚˜ ์•„๋ž˜์— ๋‚˜์—ด๋œ ํŠน์ˆ˜๋ฌธ์ž๊ฐ€ ์‚ฌ์šฉ๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.  

[ ] ( ) = , " / ? @ : ;

(2) Max age ์„ค์ • : ์ดˆ ๋‹จ์œ„๋กœ max age๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฟ ํ‚ค๊ฐ€ ์‚ด์•„์žˆ์„ ์ˆ˜ ์žˆ๋Š” ์‹œ๊ฐ„์„ ์„ค์ •ํ•˜๋Š”๊ฑฐ์ฃ . ์•„๋ž˜๋Š” 24 ์‹œ๊ฐ„๋™์•ˆ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋Š” ์ฟ ํ‚ค๋ฅผ ์„ค์ •ํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

cookie.setMaxAge(60*60*24);

(3) Cookie๋ฅผ HTTP response header์— ์ถ”๊ฐ€ํ•˜๊ธฐ : response.addCookie ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ์ถ”๊ฐ€ํ•˜ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  

response.addCookie(cookie);

์˜ˆ์ œ

<% // Create cookies for first and last names. Cookie firstName = new Cookie("first_name", request.getParameter("first_name")); Cookie lastName = new Cookie("last_name", request.getParameter("last_name")); // Set expiry date after 24 Hrs for both the cookies. firstName.setMaxAge(60*60*24); lastName.setMaxAge(60*60*24); // Add both the cookies in the response header. response.addCookie( firstName ); response.addCookie( lastName ); %> <html> <head> <title>Setting Cookies</title> </head> <body> <center> <h1>Setting Cookies</h1> </center> <ul> <li><p><b>First Name:</b> <%= request.getParameter("first_name")%> </p></li> <li><p><b>Last Name:</b> <%= request.getParameter("last_name")%> </p></li> </ul> </body> </html>

 main.jsp ํŒŒ์ผ์— ์œ„ ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ฃผ์„ธ์š”. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ ์ฝ”๋“œ๋Š” hello.jspํŒŒ์ผ์— ๋„ฃ์–ด์ฃผ์„ธ์š”. 

<html> <body> <form action="main.jsp" method="GET"> First Name: <input type="text" name="first_name"> <br /> Last Name: <input type="text" name="last_name" /> <input type="submit" value="Submit" /> </form> </body> </html>

๋‘ jspํŒŒ์ผ์„ <Tomcat-installation-directory>/webapps/ROOT ๋””๋ ‰ํ† ๋ฆฌ์— ๋„ฃ์–ด์ฃผ์„ธ์š”.

์ด์ œ http://localhost:8080/hello.jsp ์— ์ ‘์†์„ ํ•ด๋ณด์„ธ์š”.

 

 

 

์ด๋Ÿฐ๊ฒŒ ๋‚˜์˜ค๋‚˜์š”? ์ด์ œ ์ด๋ฆ„์„ ๋„ฃ๊ณ  submit๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ๋ณด์„ธ์š”. 

๊ทธ๋Ÿผ ์˜ˆ์ „์— ๋ดค๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ํผ์ŠคํŠธ ๋„ค์ž„๊ณผ ๋ž˜์ŠคํŠธ ๋„ค์ž„์ด ์ถœ๋ ฅ์ด ๋ ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ ์ด๋…€์„์ด ๋ชฐ๋ž˜ํ•˜๋Š” ์ž‘์—…์ด ๋˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ˆˆ์—๋Š” ๋ณด์ด์ง€ ์•Š์•˜์ง€๋งŒ first_name๊ณผ last_name์ด๋ผ๋Š” ์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑํ•œ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  submit๋ฒ„ํŠผ์ด ๋˜ ๋ˆŒ๋ฆฌ๊ฒŒ ๋  ๊ฒฝ์šฐ ๋ฐฉ๊ธˆ ๋งŒ๋“ค์–ด์ง„ ์ฟ ํ‚ค๋“ค์ด ์„œ๋ฒ„์ชฝ์œผ๋กœ ์ „์†ก๋˜๊ฒŒ ๋˜๊ฒ ์ฃ . 

์ž, ๊ทธ๋Ÿผ ์ด์ œ ์„œ๋ฒ„์ชฝ์œผ๋กœ ์ „์†ก๋˜๋Š” ์ฟ ํ‚ค์ •๋ณด๋ฅผ JSP์—์„œ ์ฝ์–ด์˜ค๋Š” ๊ฒƒ์„ ํ•œ๋ฒˆ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 


JSP๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฟ ํ‚ค์ •๋ณด ์ฝ์–ด์˜ค๊ธฐ

์ฟ ํ‚ค๋ฅผ ์ฝ์œผ๋ ค๋ฉด HttpServletRequest ์˜ getCookies( ) ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋Š” Cookie[]๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํ•œ๋ฒˆ์— ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ฟ ํ‚ค๊ฐ€ ์ „์†ก๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด์ฃ . ๊ทธ๋Ÿผ ๋ฃจํ”„๋ฅผ ๋Œ๋ฉด์„œ ์ „์†ก๋œ ์ฟ ํ‚ค์˜ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ํ•œ๋ฒˆ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.  


์˜ˆ์ œ

<html> <head> <title>Reading Cookies</title> </head> <body> <center> <h1>Reading Cookies</h1> </center> <% Cookie cookie = null; Cookie[] cookies = null; // Get an array of Cookies associated with this domain cookies = request.getCookies(); if( cookies != null ){ out.println("<h2> Found Cookies Name and Value</h2>"); for (int i = 0; i < cookies.length; i++){ cookie = cookies[i]; out.print("Name : " + cookie.getName( ) + ", "); out.print("Value: " + cookie.getValue( )+" <br/>"); } }else{ out.println("<h2>No cookies founds</h2>"); } %> </body> </html>

์œ„ ์ฝ”๋“œ๋ฅผ main.jsp ํŒŒ์ผ์— ๋„ฃ๊ณ  ์ ‘์†ํ•ด๋ณด์„ธ์š”. ์ฟ ํ‚ค์— ์„ค์ •ํ•œ ๊ฐ’๋“ค์ด ๋‚˜์˜ค๊ฒ ์ฃ ? ์•„๋ž˜์ฒ˜๋Ÿผ ๋ง์ด์ฃ .


Found Cookies Name and Value

Name : first_name, Value: ํ™๊ธธ๋™์€
Name : last_name, Value: ํ”Œ๋ ˆ์ด๋ณด์ด


JSP๋ฅผ ์ด์šฉํ•œ ์ฟ ํ‚ค ์‚ญ์ œ

์ด๋ฒˆ์—๋Š” ์ฟ ํ‚ค๋ฅผ ์‚ญ์ œํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ญ ์ฟ ํ‚ค์‚ญ์ œ๋Š” ๋ธŒ๋ผ์šฐ์ €์—์„œ๋„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด๊ธด ํ•˜์ง€๋งŒ ๊ทธ๋ž˜๋„ ํ•œ๋ฒˆ ์ฝ์–ด๋‚˜ ๋ณด์„ธ์š”. ์›น๊ฐœ๋ฐœ์ž๋ผ๋ฉด ์ฟ ํ‚ค ๋‹ค๋ฃจ๋Š” ๋ฒ•์€ ์ž˜ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•  ํ…Œ๋‹ˆ๊นŒ์š”. 

  • ์ฟ ํ‚ค๋ฅผ ์ฝ์–ด์™€์„œ ์ฟ ํ‚ค๊ฐ์ฒด์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

  • ์ฟ ํ‚ค์˜ ๋‚˜์ด๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค์–ด๋ฒ„๋ฆฌ์„ธ์š”. setMaxAge() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋Š”๊ฑด ์•„์‹œ์ฃ ?

  • ์ด ์ฟ ํ‚ค๋ฅผ ๋‹ค์‹œ response์— ๋„ฃ์–ด ๋˜๋Œ๋ ค๋ณด๋‚ด์„ธ์š”.


์˜ˆ์ œ

์•„๋ž˜ ์˜ˆ์ œ๋Š” "first_name" ์ฟ ํ‚ค๋ฅผ ์‚ญ์ œํ•˜๋Š” main.jsp ์†Œ์Šค์ž…๋‹ˆ๋‹ค. ์‚ญ์ œ๋œ ํ›„์— ์ด ์ฟ ํ‚ค๋ฅผ ๋‹ค์‹œ ์ฝ์–ด์˜ค๋ ค๊ณ ํ•˜๋ฉด null์ด ๋‚˜์˜ฌ๊ฒ๋‹ˆ๋‹ค.

<html> <head> <title>Reading Cookies</title> </head> <body> <center> <h1>Reading Cookies</h1> </center> <% Cookie cookie = null; Cookie[] cookies = null; // Get an array of Cookies associated with this domain cookies = request.getCookies(); if( cookies != null ){ out.println("<h2> Found Cookies Name and Value</h2>"); for (int i = 0; i < cookies.length; i++){ cookie = cookies[i]; if((cookie.getName( )).compareTo("first_name") == 0 ){ cookie.setMaxAge(0); response.addCookie(cookie); out.print("Deleted cookie: " + cookie.getName( ) + "<br/>"); } out.print("Name : " + cookie.getName( ) + ", "); out.print("Value: " + cookie.getValue( )+" <br/>"); } }else{ out.println( "<h2>No cookies founds</h2>"); } %> </body> </html>

์ด์ œ main.jsp๋ฅผ ์š”์ฒญํ•ด๋ณด์„ธ์š”. http://localhost:8080/main.jsp ์ฃผ์†Œ๋กœ ์š”์ฒญํ•˜๋ฉด ๋˜๊ฒ ์ฃ ? ๊ทธ๋Ÿผ ์•„๋ž˜์ฒ˜๋Ÿผ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ๊ฑฐ์—์š”.


Cookies Name and Value

Deleted cookie : first_name
Name : first_name, Value: John
Name : last_name, Value: Player

 

 http://localhost:8080/main.jsp ๋ฅผ ๋‹ค์‹œํ•œ๋ฒˆ ์š”์ฒญํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ฒŒ ๋  ๊ฑฐ์—์š”.

 

Found Cookies Name and Value

Name : last_name, Value: Player

 

first_name ์ฟ ํ‚ค๋Š” ์‚ญ์ œ๋˜์–ด์„œ ์•ˆ๋‚˜์˜ค๋„ค์š”.

 

์–ด๋•Œ์š”? ์‰ฝ์ฃ ์ž‰?  

 




 

 

Reference : http://www.tutorialspoint.com/jsp/jsp_cookies_handling.htm