• Home
  • About
    • ming photo

      ming

      studying

    • Learn More
    • Twitter
    • Facebook
    • Instagram
    • Github
    • Steam
  • Archive
    • All Posts
    • All Tags
    • All categories
  • categories
    • HTML+CSS+JavaScript
    • JAVA
    • Algorithm
    • DB
    • JSP
    • ์ •๋ณด์ฒ˜๋ฆฌ๊ธฐ์‚ฌ
    • Spring
    • Thymeleaf
    • ๊ธฐ์ˆ ๋ฉด์ ‘
  • Projects

JSP - NewLecture - Application, Session, Cookie

29 Mar 2021

๐Ÿ”ท 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);
                
              }
          }
    
      }
    


Share Tweet +1