๐ท NewLecture - Application, Session, Cookie
โ Application
- Application ์ ์ฅ์ : ์๋ธ๋ฆฟ ์ปจํ
์คํธ(Context)
=> ์๋ธ๋ฆฟ๋ค์ด ์๋ก๊ฐ์ ๋ฌธ๋งฅ์ ์ด์ด๊ฐ ์ ์๋๋ก ํด์ฃผ๋ ์ ์ฅ์๋ก ์ฌ์ฉ๋ ์ ์๋ ์ํ ์ ์ฅ๊ณต๊ฐ(๋๊ตฌ๋ ์ฌ์ฉํ ์ ์๋ค)
โ Session
- Session : ์ํ ์ ์ฅ๊ณต๊ฐ , ํ์ฌ ์ฌ์ฉ์
- ์น์๋ฒ(WAS)๊ฐ ํ์ฌ์ฌ์ฉ์(Session)์ ๊ตฌ๋ถํ๋ ๋ฐฉ์ : ID๋ฅผ ๊ฐ์ง๊ณ ์์ด์ผํ๋ค (ํด๋น ID๋ฅผ ๊ฐ์ง๊ณ ์๋ ์ฌ์ฉ์๋ง ์ฌ์ฉํ ์ ์๋ค)
โ Cookie
- Cookie : ํด๋ผ์ด์ธํธ๊ฐ ๊ฐ์ง๊ณ ์๋ ๊ฐ
- ์ฟ ํค ์ ์ฅํ๊ธฐ : Cookie cookie = new Cookie("c", String.valueOf(result)); => "c" : ํค, String.valueOf(result) : ๊ฐ
response.addCookie(cookie); => addCookie๋ฅผ ์ด์ฉํด ์ถ๋ ฅํ๊ณ response์ ๊ฐ(cookie)๋ฅผ ์ฌ์ด ๋ธ๋ผ์ฐ์ ๋ฅผ ์ฝ๋๋ค, (cookie) : ๋ธ๋ผ์ฐ์ ์ ์ ๋ฌํ๋ ๊ฐ
- ์ฟ ํค ์ฝ๊ธฐ : Cookie[] coolies = request.getCookies(); => getCookies() : ๋ฐฐ์ด๋ก ๊ฐ์ ๋ฐ๋๋ค
String _c = "";
if (cookies != null){
for(Cookie cookie : coookies) => ๋ฐ๋ณต๋ฌธ์ ํตํด ํค์บ์ ์ฐพ์ ํด๋นํค๊ฐ์ ํด๋น๋๋ value๋ฅผ ์ฐพ๋๋ค
if("c".equals(cookie.getName())){
_c = cookie.getValue();
}
}
โ Application, Session, Cookie ๋น๊ต
- Application ์ ์ฌ์ฉ๋ฒ์ : ์ ์ญ ๋ฒ์์์ ์ฌ์ฉํ๋ ์ ์ฅ๊ณต๊ฐ
์๋ช
์ฃผ๊ธฐ : WAS๊ฐ ์์ํด์ ์ข
๋ฃํ ๋ ๊น์ง
์ ์ฅ์์น : WAS ์๋ฒ์ ๋ฉ๋ชจ๋ฆฌ
- Session ์ ์ฌ์ฉ๋ฒ์ : ์ธ์
๋ฒ์์์ ์ฌ์ฉํ๋ ์ ์ฅ๊ณต๊ฐ
์๋ช
์ฃผ๊ธฐ : ์ธ์
์ด ์์ํด์ ์ข
๋ฃํ ๋ ๊น์ง
์ ์ฅ์์น : WAS ์๋ฒ์ ๋ฉ๋ชจ๋ฆฌ
- Cookie ์ ์ฌ์ฉ๋ฒ์ : Web Browser๋ณ ์ง์ ํ path๋ฒ์ฃผ ๊ณต๊ฐ
์๋ช
์ฃผ๊ธฐ : Browser์ ์ ๋ฌํ ์๊ฐ๋ถํฐ ๋ง๋ฃ์๊ฐ ๊น์ง
์ ์ฅ์์น : Web Browser์ ๋ฉ๋ชจ๋ฆฌ ๋๋ ํ์ผ
- ์ฝ๋(form)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>calc</title> </head> <body> <form action="calc2" method="post"> <div> <label>์ ๋ ฅ : </label> <input type="text" name="v" /> </div> <div> <input type="submit" name="operator" value="+" /> <input type="submit" name="operator" value="-" /> <input type="submit" name="operator" value="=" /> </div> </form> </body> </html>
- ์ฝ๋(java)
package newlecture; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class add */ @WebServlet("/calc2") public class calc2 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public calc2() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext application = request.getServletContext(); // Session ๊ฐ์ฒด ์์ฑ HttpSession session = request.getSession(); //์ฟ ํค ๋ณ์ ์ ์ธ Cookie[] cookies = request.getCookies(); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); String v_ = request.getParameter("v"); String op = request.getParameter("operator"); // ์ ๋ ฅ๊ฐ์ด ์์๊ฒฝ์ฐ ๋๋น int v = 0; // ํ๋ณํ if(!v_.equals("")) v = Integer.parseInt(v_); // ๊ณ์ฐ if(op.equals("=")) { //int x = (Integer)application.getAttribute("value"); //int x = (Integer)session.getAttribute("value"); // ์ฟ ํค ์ฐพ๊ธฐ int x = 0; for(Cookie c : cookies) { if(c.getName().equals("value")) { x =Integer.parseInt(c.getValue()); break; } } int y = v; //String operator = (String)application.getAttribute("op"); //String operator = (String)session.getAttribute("op"); String operator=""; for(Cookie c : cookies) { if(c.getName().equals("op")) { operator =c.getValue(); break; } } int result = 0; if(operator.equals("+")) { result = x + y; }else { result = x - y; }response.getWriter().printf("result : %d\n" , result); // ๊ฐ์ ์ ์ฅ }else { // ServletContext : applicaiont ์ ์ฅ์ //application.setAttribute("value", v); //application.setAttribute("op", op); // session.setAttribute("value", v); // session.setAttribute("op", op); // ์ฟ ํค ์์ฑ // cookie : ๋ฌธ์์ด๋ง ์ ์ฅ๊ฐ๋ฅ Cookie valueCookie = new Cookie("value",String.valueOf(v)); Cookie opCookie = new Cookie("op",op); // ์ฟ ํค๊ฐ ์ด๋ค ๊ฒฝ์ฐ์ ํด๋ผ์ด์ธํธ๋ก ์ ๋ฌ๋์ด์ผ ํ๋์ง ์ค์ //valueCookie.setPath("/"); // ("/") : ๋ชจ๋ ํ์ด์ง๋ฅผ ์์ฒญํ ๋ ๋ง๋ค valueCookie๋ฅผ ๊ฐ์ ธ์ค๋ผ๋ ์ค์ //opCookie.setPath("/"); valueCookie.setPath("/calc2"); // calc2์์๋ง ์ฟ ํค ์ ๋ฌํ๋ ์ค์ valueCookie.setMaxAge(24*60*60); // ์ฟ ํค ๋ง๋ฃ์๊ฐ ์ค์ ,(24*60*60) : 24์๊ฐ => ์ค์ ์ํ๋ฉด ๋ธ๋ผ์ฐ์ ๋ซํ๋ฉด ์ฟ ํค๊ฐ ์ญ์ ๋๋ค opCookie.setPath("/calc2"); // ์ฟ ํค ํด๋ผ์ด์ธํธ๋ก ๋ณด๋ด๊ธฐ response.addCookie(valueCookie); response.addCookie(opCookie); } } }