[JSP] Session (μ„Έμ…˜)


Session (μ„Έμ…˜)


  • 쿠킀와 λ§ˆμ°¬κ°€μ§€λ‘œ μ„œλ²„μ™€μ˜ 관계λ₯Ό μœ μ§€ν•˜κΈ° μœ„ν•œ μˆ˜λ‹¨μœΌλ‘œ, ν΄λΌμ΄μ–ΈνŠΈμͺ½μ— μ €μž₯λ˜λŠ” μΏ ν‚€μ™€λŠ” 달리 μ„Έμ…˜μ€ μ„œλ²„ 상에 객체둜 μ‘΄μž¬ν•œλ‹€. (데이터λ₯Ό μœ μ§€ν•˜κΈ° μœ„ν•œ μ €μž₯μ†Œμ˜ 기쀀이 μ„œλ²„μ— μžˆμ„ 경우 κ·Έ 데이터λ₯Ό β€˜μ„Έμ…˜β€™μ΄λΌκ³  ν•˜κ³ , ν΄λΌμ΄μ–ΈνŠΈμ— μžˆμ„ 경우 κ·Έ 데이터λ₯Ό β€˜μΏ ν‚€β€™λΌκ³  함)

  • μ›Ήμ—μ„œ λ‹€λ₯Έ νŽ˜μ΄μ§€λ‘œ μ΄λ™ν•˜κ±°λ‚˜ νŽ˜μ΄μ§€ λ‘œλ”©μ΄ λλ‚˜λ©΄ μ„œλ²„μ™€μ˜ 접속이 무쑰건 λŠμ–΄μ§€λŠ”λ°, μ„Έμ…˜μ€ μ„œλ²„μ—μ„œ μ™ΈλΆ€ 접속에 λŒ€ν•œ μ˜μ†μ„±μ„ μœ μ§€ν•˜κΈ° μœ„ν•œ κΈ°μˆ μ΄λ‹€.

  • μ„œλ²„μ—μ„œλ§Œ 접근이 κ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έμ— λ³΄μ•ˆμ— 더 κ°•ν•˜λ©°, μš©λŸ‰μ— μ œν•œμ΄ μ—†μŒ

    • μ„Έμ…˜μ΄ 쿠킀보닀 더 높은 λ³΄μ•ˆμ„±μ„ 가지고 μžˆμœΌλ‚˜ μΏ ν‚€λŠ” μƒλŒ€μ μœΌλ‘œ μ„œλ²„μ˜ λΆ€ν•˜λ₯Ό λ°œμƒμ‹œν‚€μ§€ μ•ŠμŒ
  • μ›Ή λΈŒλΌμš°μ € λ‹Ή ν•˜λ‚˜μ˜ μ„Έμ…˜μ„ 생성 (JSPμ»¨ν…Œμ΄λ„ˆ(μ„œλΈ”λ¦Ώ)κ°€ μžλ™ 생성)

  • μ„Έμ…˜μ΄ μƒμ„±λ˜λ©΄ μ„Έμ…˜μ΄ μƒμ„±λœ νŠΉμ • 값에 β€˜μ„Έμ…˜μ΄λ¦„(μ„Έμ…˜ν‚€)’, β€˜μ„Έμ…˜μ•„μ΄λ””(μ„Έμ…˜μ½”λ“œ)’, β€˜μ„Έμ…˜μΈν„°λ²Œ(μ„Έμ…˜ μœ μ§€μ‹œκ°„)’ μ„Έ 가지 ν•­λͺ©μ΄ μžλ™μœΌλ‘œ 섀정됨

  • 둜그인 정보, μ‚¬μš©μž ν™˜κ²½μ„€μ •, DB컀λ„₯μ…˜ λ“± 정보λ₯Ό μœ μ§€ν•΄μ•Ό ν•˜λŠ” 것듀에 μ‚¬μš©

  • ν΄λΌμ΄μ–ΈνŠΈμ—μ„œ μ„œλ²„ νŽ˜μ΄μ§€ 접속 -> μž…λ ₯ ν›„ νŽ˜μ΄μ§€ μ „ν™˜ μ‹œ μ„Έμ…˜ μ½”λ“œ 생성됨(λžœλ€κ°’) -> μƒμ„±λœ μ„Έμ…˜μ„ ν™•μΈν•˜λŠ” κΈ°μ€€ λ°μ΄ν„°λŠ” μ„œλ²„μ— 보관 / ν™•μΈμš© λΉ„κ΅λ°μ΄ν„°λŠ” ν΄λΌμ΄μ–ΈνŠΈλ‘œ 전솑됨

  • μ„Έμ…˜ κ΄€λ ¨ λ©”μ†Œλ“œ

    • setAttribute(String name, Object value) : μ„Έμ…˜μ— 데이터 μ €μž₯

    • getAttribute(String name) : μ„Έμ…˜ 속성이 name인 μ†μ„±μ˜ 값을 μ–»μŒ, Object νƒ€μž…μœΌλ‘œ 리턴, ν•΄λ‹Ή 속성 이름이 μ—†λŠ” 경우 null 리턴

    • getAttributeNames() : μ„Έμ…˜μ— μ €μž₯λ˜μ–΄ μžˆλŠ” λͺ¨λ“  λ°μ΄ν„°μ˜ 이름을 Enumeration 객체 νƒ€μž…μœΌλ‘œ 리턴

    • getId() : μ„Έμ…˜μ— ν• λ‹Ήλœ 고유 IDλ₯Ό String νƒ€μž…μœΌλ‘œ 리턴

    • isNew() : 졜초 μƒμ„±λœ μ„Έμ…˜μΈμ§€, 이전에 μƒμ„±λœ μ„Έμ…˜μΈμ§€ ꡬ뢄

    • setMaxInactiveInterval(int second) : μ„Έμ…˜μ˜ μœ νš¨μ‹œκ°„ μ„€μ •, κΈ°λ³Έ 30λΆ„μ΄μ§€λ§Œ λ³€κ²½ κ°€λŠ₯

    • getMaxInactiveInterval() : μ„Έμ…˜μ˜ μœ μ§€μ‹œκ°„μ„ μ–»μŒ, κ°€μž₯ 졜근 μš”μ²­μ‹œμ μ„ κΈ°μ€€μœΌλ‘œ 카운트

    • removeAttribute(String name) : μ„Έμ…˜μ—μ„œ νŠΉμ • 데이터(νŒŒλΌλ―Έν„°κ°’) 제거, μ„Έμ…˜μ„ μ œκ±°ν•˜λŠ” 것이 μ•„λ‹Œ 값을 μ‚­μ œν•˜λŠ” 것!

    • invalidate() : μ„Έμ…˜μ˜ λͺ¨λ“  데이터λ₯Ό μ‚­μ œ



μ„Έμ…˜μ„ μ‚¬μš©ν•΄ κ°„λ‹¨ν•œ 둜그인 / λ‘œκ·Έμ•„μ›ƒ νŽ˜μ΄μ§€ λ§Œλ“€κΈ° β‘ 


  • sessionLogin.html

      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="UTF-8">
      <title>둜그인</title>
      </head>
      <body>
          <form action="SessionChkLogin.jsp" method="post">
              아이디 : <input type="text" name="uid" size="10">
              <br><br>
              λΉ„λ°€λ²ˆν˜Έ : <input type="password" name="upw" size="10">
              <br><br>
              <input type="submit" value="둜그인">
              <input type="reset" value="μ΄ˆκΈ°ν™”">
          </form>
      </body>
      </html>
    
  • SessionChkLogin.jsp

     <%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
     <!DOCTYPE html>
     <html>
     <head>
     <meta charset="UTF-8">
     <title>둜그인 확인 νŽ˜μ΄μ§€</title>
     </head>
     <body>
         <%!
             String uid, upw;
         %>
         <%
             uid = request.getParameter("uid");
             upw = request.getParameter("upw");
            
             if(uid.equals("admin") && upw.equals("12345")){
                    
                 session.setAttribute("uid", uid);
                 response.sendRedirect("SessionLoginSuccess.jsp");
                    
             }
             else response.sendRedirect("SessionLoginFail.jsp");
            
         %>
    
     </body>
     </html>
    
  • SessionLoginSuccess.jsp

      <%@page import="java.util.Enumeration"%>
      <%@ page language="java" contentType="text/html; charset=UTF-8"
          pageEncoding="UTF-8"%>
      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="UTF-8">
      <title>둜그인 성곡</title>
      </head>
      <body>
          <h1>둜그인 성곡</h1>
          <%
              request.setCharacterEncoding("UTF-8");
              Enumeration<String> en = session.getAttributeNames();
                
              while(en.hasMoreElements()){
                  String name = en.nextElement();
                  String value = (String)session.getAttribute(name);
                    
                  if(value.equals("admin")){
                      out.print(value + "λ‹˜ ν™˜μ˜ν•©λ‹ˆλ‹€.");
                  }
              }
                
          %>
            
          <a href="SessionLogout.jsp"><input type="button" value="λ‘œκ·Έμ•„μ›ƒ"></a>
      </body>
      </html>
    
  • SessionLoginFail.jsp

      <%@ page language="java" contentType="text/html; charset=UTF-8"
          pageEncoding="UTF-8"%>
      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="UTF-8">
      <title>둜그인 μ‹€νŒ¨</title>
      </head>
      <body>
          <script type="text/javascript">
              alert('둜그인 μ‹€νŒ¨');
              location.href="SessionLogin.html";
          </script>
      </body>
      </html>
    
  • SessionLogout.jsp

      <%@page import="java.util.Enumeration"%>
      <%@ page language="java" contentType="text/html; charset=UTF-8"
          pageEncoding="UTF-8"%>
      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="UTF-8">
      <title>λ‘œκ·Έμ•„μ›ƒ</title>
      </head>
      <body>
          <%
              Enumeration<String> en = session.getAttributeNames();
                
              while(en.hasMoreElements()){
                  String name = en.nextElement();
                  String value = (String)session.getAttribute(name);
                    
                  if(value.equals("admin")){
                      out.print("λ‘œκ·Έμ•„μ›ƒν•˜μ…¨μŠ΅λ‹ˆλ‹€.");
                
                      session.invalidate();
                      // session.removeAttribute(name);
                  }
              }
          %>
          <a href="sessionLogin.html"><input type="button" value="λ‹€μ‹œλ‘œκ·ΈμΈν•˜κΈ°"></a>
      </body>
      </html>
    



μ„Έμ…˜μ„ μ‚¬μš©ν•΄ κ°„λ‹¨ν•œ 둜그인 / λ‘œκ·Έμ•„μ›ƒ νŽ˜μ΄μ§€ λ§Œλ“€κΈ° β‘‘


  • login.html

      <!DOCTYPE html>
      <html lang="ko">
      <head>
          <meta charset="UTF-8">
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>둜그인 ν™”λ©΄</title>
      </head>
      <body>
          <div id="wrap">
              <h1>Session Example</h1>
              <form action="LoginProc.jsp">
                  아이디 <input type="text" name="uid">
                  <br><br>
                  λΉ„λ°€λ²ˆν˜Έ <input type="password" name="upw">
                  <br><br>
                  <button>둜그인</button>
                  <!-- λ²„νŠΌ κΈ°λ³Έ type=submit -->
              </form>
          </div>
          <!-- div#wrap -->
      </body>
      </html>
    
  • LoginProc.jsp

      <%@ page language="java" contentType="text/html; charset=UTF-8"
          pageEncoding="UTF-8"%>
      <%
          request.setCharacterEncoding("UTF-8");
          String uid = request.getParameter("uid");
          String upw = request.getParameter("upw");
    
          /* μ„Έμ…˜ 생성 > 이 νŽ˜μ΄μ§€λ₯Ό μ‹€ν–‰ ν•œ λΈŒλΌμš°μ €(μ ‘μ†ν•œ λΈŒλΌμš°μ €)λŠ”
                          μ„Έμ…˜(=μ„Έμ…˜κ°’, μ„Έμ…˜μ½”λ“œ)을 κ°–κ²Œ 됨,
                          μ„Έμ…˜μ€ νŠΉμ • 값에 패치(patch)됨
          */
          session.setAttribute("idKey", uid);
          session.setMaxInactiveInterval(30); 
          //30μ΄ˆκ°„ μ„Έμ…˜ μœ μ§€
          response.sendRedirect("Session_Result.jsp");
          // μ„Έμ…˜ λ§Œλ“€κ³  λ°”λ‘œ νŽ˜μ΄μ§€ 이동
            
      %>
    
  • Session_Result.jsp

      <%@ page language="java" contentType="text/html; charset=UTF-8"
          pageEncoding="UTF-8"%>
      <!DOCTYPE html>
      <html lang="ko">
      <head>
          <meta charset="UTF-8">
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>둜그인 처리 ν›„ 처리 κ²°κ³Ό 확인</title>
      </head>
      <body>
    
          <div id="wrap">
              <h1>μ„Έμ…˜ μƒνƒœ 보기 νŽ˜μ΄μ§€</h1>
              <%
                  String uid = (String)session.getAttribute("idKey");
                  String sessionID = session.getId();
                  int sessionIntervalTime = session.getMaxInactiveInterval();
                  // 30μ΄ˆκ°€ μ§€λ‚˜κΈ° 전에 μƒˆλ‘œκ³ μΉ¨ν•˜λ©΄ λ‹€μ‹œ μ„Έμ…˜ μœ μ§€ 30초 μ‹œμž‘
                    
                  if(uid!=null){
                      //μ„Έμ…˜μ΄ μ‚΄μ•„μžˆμŒ = μ„Έμ…˜κ°’μ΄ μœ μ§€λ˜κ³  있음
                        
                      out.println(uid + "λ‹˜κ»˜μ„œ 둜그인 μƒνƒœμž…λ‹ˆλ‹€.");
                      out.println("<a href='LogoutProc.jsp'>λ‘œκ·Έμ•„μ›ƒ</a>");
                  } else {
                        
              %>
                  <script>
                      alert("μ„Έμ…˜μ΄ μ†Œλ©Έλ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
                      location.href = "login.html";
                  </script>
              <%
                  }
              %>
          </div>
    
      </body>
      </html>
    
  • LogoutProc.jsp

      <%@ page language="java" contentType="text/html; charset=UTF-8"
          pageEncoding="UTF-8"%>
    
      <%
          session.invalidate(); //μ„Έμ…˜ μ†Œλ©Έ
          response.sendRedirect("Session_Result.jsp");
      %>
    

Categories:

JSP/Servlet