Cookie & Session
์ํ์ ์ง (Cookie & Session)
Web์ ๊ธฐ์ ์ ํด๋ผ์ด์ธํธ๋ฅผ ํตํด ๋ค์ด์จ ์ ๋ณด๋ฅผ ์ ์ํ ํ๋ก์ธ์ ์ข
๋ฃ์ ์ ์์ ์ข
๋ฃํ๋ ํน์ฑ์ ๊ฐ์ง๊ณ ์๋ค.
์ด๋ฌํ ํน์ฑ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฐจ์งํ์ง ์๋๋ค๋ ์ฅ์ ์ ๊ฐ์ง๊ณ ์์ง๋ง Web๊ธฐ์ ์ ๋ฐ๋ฌํจ์ผ๋ก์จ Web์ ์ด๋ฌํ ํน์ฑ์ด ๋จ์ ์ผ๋ก ์์ฉํ๋๋ฐ
์ํ์ ์ง๋ฅผ ์ํ ๊ธฐ์ ๋ก Cookie & Session์ด๊ฐ ๋ฑ์ฅํ๋ค.
Cookie
- ํด๋ผ์ด์ธํธ ์ปดํจํฐ์ ์ ์ฅํ๋ ๊ธฐ์
- ์ ์ฅ๋ ์ ๋ณด๋ฅผ ๋ค๋ฅธ ์ฌ๋ ๋ํ ์ฝ๊ฒ ์ ๊ทผ๊ฐ๋ฅํ ๋จ์ ์ ๊ฐ์ง๋ค.
- ์ ํจ์๊ฐ์ด ์ง๋๋ฉด ์ฌ๋ผ์ง๋ค.
- ์๋ฒ ์ ๋ณด์ ์ ๊ทผ์ ์๋ณ์ฑ์ ๊ฐ์ง์ง ์์ ์ํ๋ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ธฐ ์ด๋ ค์
Session
- ์๋ฒ์ ์ ์ฅํ๋ ๊ธฐ์
- ์๋ฒ๊ฐ ์ข ๋ฃ๋๊ฑฐ๋ ์ ํจ์์ด ์ง๋๋ฉด ์ฌ๋ผ์ง๋ค.
- ํน๋ณํ ์๋ณID๋ฅผ ๊ฐ์ง์ผ๋ก์จ ์๋ฒ์์ ์ํ๋ ์ ๋ณด๋ง์ ๊ฐ์ง์ ์๋ค.
Cookie
๋์์ ์ดํด img
- ํด๋ผ์ด์ธํธ๊ฐ ์ ๋ณด๋ฅผ ์ ๋ ฅํ๊ณ request์ ์ ๋ ฅ ์ ๋ณด๋ฅผ ๊ฐ์ง๋ ์ฟ ํค๋ฅผ ์์ฑํ๋๋ฐ Key์ Value์ธ Map๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋ค.
- respone์ ๋ง๋ค์ด์ง ์ฟ ํค์ ํจ๊ป ํด๋ผ์ด์ธํธ์๊ฒ ๋ณด๋ด์ง๋ฉฐ ์ฟ ํค๋ ํด๋ผ์ด์ธํธ ์ปดํจํฐ์ ์ ์ฅ๋๋ค.
- ๋ค์ request์ ๋ง๋ค์ด์ง ์ฟ ํค๋ฅผ ํฌํจํ์ฌ ๊ฐ์ด ์๋ฒ๋ก ๋ณด๋ด์ง๋ค.
- ๋ง์ฝ ๋์ผํ ์ฟ ํค๊ฐ ์กด์ฌํ๋ค๋ฉด ์์ฑํ์ง ์๊ณ ๊ทธ ์ฟ ํค์ ๋ํ ์ ๋ณด๋ฅผ ๋ณผ ์ ์๊ฒ๋๋ค.
์๋ฐ์์ Cookie ์์ฑ
//Cookie ์์ฑ(name=value) :: ํ๊ธ ์ธ์ฝ๋ฉ ํ ์ ์ฅ
Cookie cookie = new Cookie("name",URLEncoder.encode("ํ๊ธธ๋"));
cookie.setMaxAge(60*60); //cookie ์ ํจ๊ธฐ๊ฐ(์ด) ์ง๋๋ฉด ์ฟ ํค ์ญ์
//cookie.setMaxAge(-1); //cookie memory ์ ์ฅ :: ?? ==> APIํ์ธ
//cookie.setMaxAge(0); //cookie 0์ด๋์ ์ ํจ :: ?? ==> APIํ์ธ
res.addCookie(cookie); //Client ๋ก response ์ธ์คํด์ค๋ฅผ ์ฌ์ฉ cookie ์ ์ก
out.println("<html><body>");
out.println("Cookie ์ ์ฅ ์๋ฃ");
out.println("</body></html>");
Cookie์ ์ ๋ณด
// Client ๋ก ๋ถํฐ ์ ์ก๋ Cookie ์ฒ๋ฆฌ
Cookie[] cookies = req.getCookies();
// Cookie ์ name = value ์ฒ๋ฆฌ ๋ณ์ (Map๊ตฌ์กฐ)
String userName =null;
//Cookie ์ ์กด์ฌ์ ๋ฌด ๋ฐ name=value ์ฒ๋ฆฌ
if(cookies != null) {
out.println("Client์์ ์ ์ก๋ Cookie ์์ต๋๋ค.<br/>");
//Array ๋ก return :: Array ๊ฐฏ์๋งํผ ์ฒ๋ฆฌ
for(int i=0;i<cookies.length;i++){
//name = value ํ์์ ์ ์ฅ๊ฐ ์ค name ์ถ์ถ
String name = cookies[i].getName();
String value = URLDecoder.decode(cookies[i].getValue());
System.out.println("client๋ก ๋ถํฐ ์ ์ก๋ cookie : "+name+"="+value);
if(name.equals("name")){ // Key๊ฐ์ค "name"์ value๋ก userName ์ค์
userName = value;
}
}
}else{
out.println("Client์์ ์ ์ก๋ cookie๊ฐ ์์ต๋๋ค.<br/>");
}
out.println("<html><body>");
//userName์ด null ์๋ฏธ :: cookie ์ id ๊ฐ ์ ์ฅ ๋์ง ์์์ ==> ์ฒ์๋ฐฉ๋ฌธ
if(userName == null){
out.println("์ฒ์์
๋๋ค.");
}else{
out.println(userName+"๋ ํ์");
}
out.println("</body></html>");
}
Session
๋์์ ์ดํด
Cookie๋ฅผ ์ด์ฉํ์ฌ ์๋ฒ์ ์ ์ฅํ๋ ๋ฐฉ์์ด Session์ด๋ค. img
- ์ฟ ํค๊ฐ ์๋ฒ์์ ๋ง๋ค์ด์ง ๋ ํน๋ณํ ์๋ณID๊ฐ ์์ฑ๋์ด ์ฟ ํค์ ํฌํจ๋๋ค.
- ์ฟ ํค์ ๊ฐ์ ๋ฐฉ์์ผ๋ก respone์ ์ฟ ํค์ ํจ๊ป ํด๋ผ์ด์ธํธ๋ก ๋ณด๋ด์ง๋ค.
- request์ ์ฟ ํค์ ํจ๊ป ์๋ฒ๋ก ๋ณด๋ด์ง๋๋ฐ ์ฟ ํค์๋ ํน๋ณํ ์๋ณID๊ฐ ์๊ธฐ์
์๋ฒ์ ์ ์ฅ๋์ด์๋ ์ ๋ณด๋ฅผ ๋ณผ ์ ์๋ค.
img ์์ ๊ฐ์ ๋ก์ง์ ๊ฐ์ง๊ณ ์๋ค.
Session์ ์์ฑ
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
HttpSession session = req.getSession(true);
// true์ ์ธ์
์ ์์ฑํ๋ค.
if(session.isNew()) { // ์ธ์
์ด ์๋ก ๋ง๋ค์ด์ก๋ค๋ฉด true
session.setAttribute("name", new String("ํ๊ธธ๋")); // Key , Value ๋ก ์
ํ
}
req.setCharacterEncoding("EUC_KR");
res.setContentType("text/html;charset=EUC_KR");
PrintWriter out = res.getWriter();
out.println("<html><head></head><body>");
out.println("<center><h2>SessionUseCookieOne</h2></center>");
System.out.println("\n UNIQUEํ JSESSIONID๋ : "+session.getId());
// ์๋ณID๋ฅผ ๋ถ๋ฌ์จ๋ค
if (session.isNew()) {
out.println("์ธ์
์ด ์๋ก ์์ฑ๋จ<br>");
} else {
out.println("\n UNIQUEํ JSESSIONID๋ : "+session.getId()+"์ฌ์ฉ์ค");
}
out.println("<hr>");
out.println("<a href='/edu/SessionUseCookieTwo'>๋งํฌ</a>");
out.println("</body></html>");
}
Session์ ์ ๋ณด
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
req.setCharacterEncoding("EUC_KR");
res.setContentType("text/html;charset=EUC_KR");
PrintWriter out = res.getWriter();
Cookie[] cookies = req.getCookies();
if(cookies != null) {
for(int i = 0; i<cookies.length; i++) {
System.out.print("\nCookie์ ์ ์ฅ๋ ์ ๋ณด : ");
System.out.print(cookies[i].getName()+" : "+cookies[i].getValue());
System.out.print("์ ๋ณด ํ์ธ");
System.out.print("\n");
}
}
HttpSession session = req.getSession(false);
out.println("<html><head></head><body>");
out.println("<center><h2>SessionUseCookieTwo</h2></center>");
if (session != null) {
out.println("<hr> JSESSIONID = "+session.getId()+"<hr>");
String name = (String)session.getAttribute("name");
out.println("์ด๋ฆ : "+name);
} else {
out.println("์ฒ์์ด์ญ๋๋ค.");
}
out.println("</body></html>");
}
Leave a comment