πŸ’» Programming/JSP

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

μ΄λ²ˆμ—λŠ” JSP둜 μ„Έμ…˜μ„ λ‹€λ£¨λŠ” 법에 λŒ€ν•΄μ„œ λ°°μ›Œλ³΄λ„λ‘ ν•˜κ² μŠ΅λ‹ˆλ‹€. HTTPλŠ” "stateless" ν”„λ‘œν† μ½œμž…λ‹ˆλ‹€.  무슨 λ§μ΄λƒν•˜λ©΄ 말이죠, ν΄μ•„μ΄μ–ΈνŠΈ(μ›ΉλΈŒλΌμš°μ €)κ°€ μ›ΉνŽ˜μ΄μ§€λ₯Ό λΆˆλŸ¬μ˜¬λ•Œλ§ˆλ‹€ ν΄λΌμ΄μ–ΈνŠΈλŠ” μ›Ήμ„œλ²„λ‘œ λ³„λ„μ˜ 컀λ„₯μ…˜μ„ λ§ΊμŠ΅λ‹ˆλ‹€. 그리고 이전 ν΄λΌμ΄μ–ΈνŠΈμ˜ 접속에 λŒ€ν•œ μ–΄λ– ν•œ 정보도 μžλ™μœΌλ‘œ κΈ°λ‘ν•˜μ§€λŠ” μ•ŠμŠ΅λ‹ˆλ‹€. 

μ–΄μ¨Œλ“  μ„Έμ…˜μ΄λΌλŠ” 것은 λ°”λ‘œ μ›ΉλΈŒλΌμš°μ €κ°€ μ›Ήμ„œλ²„μ™€ 연결이 λ λ•Œ μƒκΈ°λŠ” 것인데, 이 μ„Έμ…˜μ„ μœ μ§€ν•˜λŠ” λ°©λ²•μ—λŠ” μ—¬λŸ¬κ°€μ§€κ°€ μžˆλ‹΅λ‹ˆλ‹€.  

1. Cookies

쿠킀에 μ„Έμ…˜IDλ₯Ό μ €μž₯ν•˜λŠ” μ‹μœΌλ‘œ μ„Έμ…˜μ„ 관리할 μˆ˜λŠ” μžˆκ² μ§€λ§Œ λΈŒλΌμš°μ €μ—μ„œ μΏ ν‚€μ‚¬μš© μ œν•œ 섀정이 μžˆμ„ 수 μžˆκΈ°λ•Œλ¬Έμ— μ„Έμ…˜μ„ μΏ ν‚€λ₯Ό μ΄μš©ν•΄μ„œ κ΄€λ¦¬ν•œλ‹€λŠ” 것은 쒋은방법이 μ•„λ‹™λ‹ˆλ‹€.

 

 

2. Hidden Form Fields

 

μ›Ήμ„œλ²„λŠ” hidden HTML form field에 unique session IDλ₯Ό λ„£μ–΄μ„œ μ•„λž˜μ²˜λŸΌ 전솑할 수 μžˆμŠ΅λ‹ˆλ‹€.

<input type="hidden" name="sessionid" value="12345">

form이 전솑될 λ•Œ μ„Έμ…˜ID와 값을 λ„˜κ²¨μ£ΌλŠ”κ±°μ£ . λΈŒλΌμš°μ €μ—μ„œ 이 정보λ₯Ό μ›Ήμ„œλ²„μ— 보내면 이 정보λ₯Ό 가지고 μ›Ήμ„œλ²„μ—μ„œλŠ” λ‹€λ₯Έ λΈŒλΌμš°μ €μ—μ„œ μ ‘μ†ν•œ 것인지λ₯Ό νŒλ‹¨ν•  수 있게 λ©λ‹ˆλ‹€. ν•˜μ§€λ§Œ 이 μ—­μ‹œ 쒋은 방법은 μ•„λ‹ˆμ£ . <a> νƒœκ·Έλ₯Ό μ΄μš©ν•΄μ„œ 링크λ₯Ό 타고 λ“€μ–΄μ˜€λŠ” κ²½μš°μ—λŠ” μ € form데이타λ₯Ό μ „μ†‘ν•˜μ§€ μ•Šκ±°λ“ μš”.

3. URL Rewriting

URL 끝에 μ„Έμ…˜μ— λŒ€ν•œ 정보λ₯Ό μΆ”κ°€ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€. 그리고 μ„œλ²„λŠ” κ·Έ 정보λ₯Ό 가지고 μ„Έμ…˜μ„ 비ꡐ할 수 있겠죠.

예λ₯Όλ“€λ©΄, http://tutorialspoint.com/file.htm;sessionid=12345 μ΄λ ‡κ²Œ μš”μ²­μ„ 보내면 μ„œλ²„μ—μ„œ sessionid=12345뢀뢄에 λŒ€ν•œ 정보λ₯Ό 가지고 μ›Ήμ„œλ²„μ˜ μ„Έμ…˜μ •λ³΄μ™€ 비ꡐλ₯Ό ν•˜λŠ”κ±°μ£ .

URL rewriting 은 μ„Έμ…˜μ„ κ΄€λ¦¬ν•˜κΈ°μœ„ν•œ 방법듀 μ€‘μ—μ„œλŠ” κ·Έλ‚˜λ§ˆ 쒋은 방법이긴 ν•©λ‹ˆλ‹€. λΈŒλΌμš°μ €κ°€ μΏ ν‚€μ‚¬μš©μ„ μ•ˆν• λ•Œλ„ μ‚¬μš©κ°€λŠ₯ν•œ λ°©λ²•μ΄λ‹ˆκΉŒμš”. ν•˜μ§€λ§Œ μ—­μ‹œ 결점이 μžˆμŠ΅λ‹ˆλ‹€. μ„Έμ…˜IDλ₯Ό λ™μ μœΌλ‘œ 맀번 μƒμ„±ν•΄μ€˜μ•Ό ν•˜κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€. 정적인 HTMLνŽ˜μ΄μ§€λΌλ„ 말이죠.  


The session Object:

μœ„ 세가지 방법 이외에도 JSPλŠ” HttpSession μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 기본적으둜 JSPsλŠ” μ„Έμ…˜μ„ μΆ”μ ν•˜κ²Œ λ˜μ–΄μžˆμœΌλ©° μƒˆλ‘œμš΄ HttpSession 객체가 μƒˆλ‘œμš΄ ν΄λΌμ΄μ–ΈνŠΈλ“€μ΄ 접속할 λ•Œλ§ˆλ‹€ μžλ™μœΌλ‘œ μƒμ„±λ©λ‹ˆλ‹€. μ„Έμ…˜ 좔적 κΈ°λŠ₯을 끄렀면 page directive의 μ„Έμ…˜ 속성을 false둜 μ„ΈνŒ…ν•΄μ€˜μ•Ό ν•©λ‹ˆλ‹€. μ•„λž˜ 처럼 말이죠.

<%@ page session="false" %>

JSP engine은 JSPκ°œλ°œμžλ“€μ—κ²Œ HttpSession 객체λ₯Ό ν†΅ν•΄μ„œ session에 μ ‘κ·Όν•  수 μžˆλ„λ‘ ν•΄μ€λ‹ˆλ‹€. session κ°μ²΄κ°€ 기본적으둜 제곡이 되기 λ•Œλ¬Έμ— κ°œλ°œμžλ“€μ€ ꢂ이 μ„Έμ…˜κ°μ²΄λ₯Ό μƒμ„±ν•˜κ±°λ‚˜ getSession() λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•΄μ„œ μ–»μ–΄μ˜¬ ν•„μš”κ°€ μ—†μŠ΅λ‹ˆλ‹€.

μ›Ήμ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ μž‘μ„±ν•˜κ³  μžˆλ‹€λ©΄ κ·Έλƒ₯ μ„Έμ…˜μ„ νŒŒλΌλ―Έν„°λ‘œ λ°›μ•„μ„œ μ“°λ©΄ λ˜λŠ” κ²λ‹ˆλ‹€. 

 

λ‹€μŒμ€ μ„Έμ…˜ 객체가 가지고 λ©”μ†Œλ“œ λͺ©λ‘μž…λ‹ˆλ‹€. μ–΄λ–€ λ©”μ†Œλ“œλ“€μ΄ μžˆλŠ”μ§€ ν•œλ²ˆ μ‚΄νŽ΄λ³ΌκΉŒμš”~

 

S.N.Method & Description
1public Object getAttribute(String name)
This method returns the object bound with the specified name in this session, or null if no object is bound under the name.
2public Enumeration getAttributeNames()
This method returns an Enumeration of String objects containing the names of all the objects bound to this session.
3public long getCreationTime()
This method returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT.
4public String getId()
This method returns a string containing the unique identifier assigned to this session.
5public long getLastAccessedTime()
This method returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT.
6public int getMaxInactiveInterval()
This method returns the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses.
7public void invalidate()
This method invalidates this session and unbinds any objects bound to it.
8public boolean isNew(
This method returns true if the client does not yet know about the session or if the client chooses not to join the session.
9public void removeAttribute(String name)
This method removes the object bound with the specified name from this session.
10public void setAttribute(String name, Object value) 
This method binds an object to this session, using the name specified.
11public void setMaxInactiveInterval(int interval)
This method specifies the time, in seconds, between client requests before the servlet container will invalidate this session.


Session Tracking Example:

λ‹€μŒ μ˜ˆμ œλŠ” HttpSession 객체λ₯Ό μ΄μš©ν•˜μ—¬ μ„Έμ…˜μ˜ μƒμ„±μ‹œκ°„κ³Ό λ§ˆμ§€λ§‰ μ ‘κ·Όμ‹œκ°„ 정보λ₯Ό κ°€μ Έμ˜€λŠ” 방법에 λŒ€ν•΄μ„œ μ„€λͺ…ν•΄μ£Όκ³  μžˆμŠ΅λ‹ˆλ‹€.  

<%@ page import="java.io.*,java.util.*" %> <% // Get session creation time. Date createTime = new Date(session.getCreationTime()); // Get last access time of this web page. Date lastAccessTime = new Date(session.getLastAccessedTime()); String title = "Welcome Back to my website"; Integer visitCount = new Integer(0); String visitCountKey = new String("visitCount"); String userIDKey = new String("userID"); String userID = new String("ABCD"); // 처음 μ ‘μ†ν•œ 방문자라면 μ„Έμ…˜μ„ μƒμ„±ν•©λ‹ˆλ‹€. if (session.isNew()){ title = "Welcome to my website"; session.setAttribute(userIDKey, userID); session.setAttribute(visitCountKey, visitCount); } visitCount = (Integer)session.getAttribute(visitCountKey); visitCount = visitCount + 1; userID = (String)session.getAttribute(userIDKey); session.setAttribute(visitCountKey, visitCount); %> <html> <head> <title>Session Tracking</title> </head> <body> <center> <h1>Session Tracking</h1> </center> <table border="1" align="center"> <tr bgcolor="#949494"> <th>Session info</th> <th>Value</th> </tr> <tr> <td>id</td> <td><% out.print( session.getId()); %></td> </tr> <tr> <td>Creation Time</td> <td><% out.print(createTime); %></td> </tr> <tr> <td>Time of Last Access</td> <td><% out.print(lastAccessTime); %></td> </tr> <tr> <td>User ID</td> <td><% out.print(userID); %></td> </tr> <tr> <td>Number of visits</td> <td><% out.print(visitCount); %></td> </tr> </table> </body> </html>

 main.jsp​에 μœ„ μ½”λ“œλ₯Ό λ„£κ³  http://localhost:8080/main.jsp λ₯Ό ν˜ΈμΆœν•΄λ³΄μ„Έμš”. μ„œλ²„ μ‹€ν–‰μ‹œν‚€λŠ” 것 μžŠμ–΄λ¨Ήμ§€ λ§ˆμ‹œκ΅¬μš”~ ^___^ 

Welcome to my website

Session Infomation

Session infovalue
id0AE3EC93FF44E3C525B4351B77ABB2D5
Creation TimeTue Jun 08 17:26:40 GMT+04:00 2010
Time of Last AccessTue Jun 08 17:26:40 GMT+04:00 2010
User IDABCD
Number of visits0

 

 

μœ„μ™€κ°™μ€ νŽ˜μ΄μ§€κ°€ λ‚˜μ˜€λ‚˜μš”??

 

λ‹€μ‹œν•œλ²ˆ 호좜 ν•΄ λ³ΌκΉŒμš”??

 

Welcome Back to my website

Session Infomation

info typevalue
id0AE3EC93FF44E3C525B4351B77ABB2D5
Creation TimeTue Jun 08 17:26:40 GMT+04:00 2010
Time of Last AccessTue Jun 08 17:26:40 GMT+04:00 2010
User IDABCD
Number of visits1


μœ„μ™€ 같은 κ²°κ³Όκ°€ λ‚˜μ˜€λ‚˜μš”? Welcome λ©”μ‹œμ§€κ°€ Welcome Back λ©”μ‹œμ§€λ‘œ λ°”λ€Œμ—ˆλ„€μš”. ^__^ 

 

 

이제 μ„Έμ…˜ 데이타λ₯Ό μ‚­μ œν•˜λŠ” 방법을 μ•Œμ•„λ³ΌκΉŒμš”??​​

 

Deleting Session Data:

μ‚¬μš©μžμ˜ μ„Έμ…˜ λ°μ΄νƒ€λ‘œ ν•΄μ•Όλ˜λŠ” μž‘μ—…μ„ λͺ¨λ‘ μ™„λ£Œν•˜μ˜€λ‹€λ©΄ μ•„λž˜μ™€ 같은 μž‘μ—…μ„ λ§ˆμ§€λ§‰μœΌλ‘œ 해쀄 수 μžˆμŠ΅λ‹ˆλ‹€.

  • νŠΉμ • 속성 μ‚­μ œ : removeAttribute(String name) λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•΄μ„œ μ„Έμ…˜μ˜ νŠΉμ • μ†μ„±κ°’λ§Œ μ‚­μ œν•  수 μžˆμŠ΅λ‹ˆλ‹€. 

  • μ„Έμ…˜ μ‚­μ œ : invalidate() λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜μ—¬ μ„Έμ…˜ 전체λ₯Ό λ¬΄νš¨ν™” μ‹œν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€. μ„Έμ…˜μ„ λŠλŠ”λ‹€κ³  ν‘œν˜„ν•˜μ£ . 

  • μ„Έμ…˜ νƒ€μž„μ•„μ›ƒ μ„€μ • : setMaxInactiveInterval(int interval) λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•˜μ—¬ μ„Έμ…˜λ³„λ‘œ νƒ€μž„μ•„μ›ƒμ„ μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

  • μ‚¬μš©μž λ‘œκ·Έμ•„μ›ƒ : servlets 2.4λ₯Ό μ§€μ›ν•˜λŠ” μ„œλ²„λΌλ©΄ logout μ„ ν˜ΈμΆœν•˜μ—¬ μ‚¬μš©μžλ₯Ό λ‘œκ·Έμ•„μ›ƒμ‹œν‚€κ³  λͺ¨λ“  μ„Έμ…˜μ„ λ¬΄νš¨ν™” μ‹œν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€.  

  • web.xml μ„€μ • : Tomcat을 μ‚¬μš©μ€‘μ΄λΌλ©΄ web.xml νŒŒμΌμ—μ„œ μ•„λž˜μ²˜λŸΌ νƒ€μž„μ•„μ›ƒ μ‹œκ°„μ„ μ„€μ •ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

<session-config> <session-timeout>15</session-timeout> </session-config>

μ—¬κΈ°μ„œ νƒ€μž„μ•„μ›ƒ μ‹œκ°„μ˜ λ‹¨μœ„λŠ” λΆ„ λ‹¨μœ„μ΄λ©° ν†°μΊ£μ˜ κΈ°λ³Έ νƒ€μž„μ•„μ›ƒμΈ 30 뢄을 μ˜€λ²„λΌμ΄λ“œν•˜μ—¬ μ μš©λ©λ‹ˆλ‹€.

μ„œλΈ”λ¦Ώμ˜ getMaxInactiveInterval( )λ©”μ†Œλ“œλŠ” μ΄ˆλ‹¨μœ„λ‘œ νƒ€μž„μ•„μ›ƒ μ‹œκ°„μ„ κ°€μ Έμ˜΅λ‹ˆλ‹€. λ”°λΌμ„œ web.xml νŒŒμΌμ— 15 λΆ„μœΌλ‘œ μ„€μ •λ˜μ–΄μžˆλ‹€λ©΄  getMaxInactiveInterval( ) λ©”μ†Œλ“œλŠ” 900 ( 15λΆ„ * 60초 )λ₯Ό λ°˜ν™˜ν•˜κ²Œ λ©λ‹ˆλ‹€ .

 

 

 

 

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