Session이란?
브라우저를 종료하기 전까지
유지되는 Parameter를 말한다.
웹 서버를 껐다가 키더라도
브라우저를 종료하지 않는다면 유지된다.
기본 만료시간은 30분이며
개발자가 임의로 조정할 수 있다.
index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Session을 알아보자</title> </head> <body> <h1>Session 수업</h1> <h3><a href="./L01SetSession.jsp">Session 세팅</a></h3> <h3><a href="./L02GetSession.jsp">Session 값 얻어오기</a></h3> <h3><a href="./L03GetSessions.jsp">Session 모든 값 얻어오기</a></h3> <h3><a href="./L04InfoSession.jsp">Session 정보</a></h3> <h3><a href="./L05RemoveSession.jsp">Session 삭제</a></h3> <h3><a href="./login/L01LoginForm.jsp">Session을 이용한 로그인</a></h3> </body> </html> | cs |
Session을 설정하는 방법.
L01SetSession.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>세션 세팅</title> </head> <body> <h1>세션 설정</h1> <% //HttpSession session = request.getSession(false);//생략되어 있음 session.setAttribute("id", "jsp");//Attribute는 모두 Object session.setAttribute("pwd", 1234); session.setAttribute("name", "tako"); out.print("<h3>설정완료</h3>"); %> <p><a href="javascript:history.go(-1)">뒤로가기</a></p> </body> </html> | cs |
Session의 값을 얻어오는 방법.
L02GetSession.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>세션 값 얻어오기</title> </head> <body><!-- session = 유지되고 있는 parameter --> <h1>세션 값</h1> <!-- 세션은 브라우저가 종료될 때까지 유지된다. --> <!-- 웹 서버를 껐다가 켜도 브라우저를 종료하지 않으면 유지됨. --> <h3>id : <%=session.getAttribute("id")%></h3> <h3>pwd : <%=session.getAttribute("pwd")%></h3> <h3>name:<%=session.getAttribute("name") %></h3> <p><a href="javascript:history.go(-1)">뒤로가기</a></p> </body> </html> | cs |
Session의 모든 값을 얻어오는 방법.
L03GetSessions.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.Enumeration"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>세션 모든 값 얻어오기</title> </head> <body> <h1>세션에 설정된 모든 값</h1> <% Enumeration<String> names = session.getAttributeNames(); //key값들을 받아오는 함수 while(names.hasMoreElements()){ String name = names.nextElement().toString(); %> <h3><%=name%> : <%=session.getAttribute(name)%></h3> <% } %> <p><a href="javascript:histroy.go(-1)">뒤로가기</a></p> </body> </html> | cs |
Session의 정보를 확인하는 방법.
L04InfoSession.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>세션이 가진 정보를 보자</title> </head> <body> <h1>세션 정보</h1> <h3>1.세션 아이디 : <%=session.getId()%></h3> <h3>2.세션 생성 여부 : <%=session.isNew()%></h3> <% //특정 세션 만료시간 설정 (분*60) session.setMaxInactiveInterval(5*60);//5분으로 설정 long lastTime = session.getLastAccessedTime(); long creationTime = session.getCreationTime(); long usedTime = (lastTime-creationTime)/60000;//분 long inactive = session.getMaxInactiveInterval()/60;//분 %> <h3>3.세션 유효 시간 : <%=inactive%>분</h3> <h3>4.웹사이트에 머문 시간 : <%=usedTime%>분</h3> </body> </html> | cs |
1.세션 아이디
서버가 브라우저(클라이언트)를 구분하기 위해 붙이는 고유 ID를 말한다.
클라이언트가 서버에 접속하면 바로 생성되며, 서버가 종료되거나 강제로 만료시킬 때까지 유지된다.
2.세션 생성 여부
클라이언트가 서버에 접속하면 최초 접속시 Session이 생성되는데,
이 때 생성되면 true를 반환한다.(false를 반환할 시 최초 접속이 아님)
Session은 page에 생성되는 것이 아니라 프로젝트 기준으로 생성되며, 프로젝트 내부에서 모든 Session은 공유된다.
3. 세션 유효 시간
LastAccessedTime = 가장 최근에 접속한 시간.
CreationTime = 세션이 생성된 시간.
usedTime = 웹페이지를 사용한 시간(LastAccessedTime에서 CreationTime을 뺀 값)
MaxInactiveInterval = 세션의 유지시간. (default 30분으로 설정되어 있다.)
전체 세션 만료시간 설정하는 방법.
web.xml에 session-timeout 지정하기
<session-config>
<session-timeout>분</session-timeout> 지정하기
</session-config>
또는 위 예제에서 처럼
session.setMaxInactiveInterval(5*60);//5분으로 설정
와 같이 설정할 수 있다.
세션 삭제하기.
L05RemoveSession.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>세션의 값을 삭제하자</title> </head> <body> <h1>세션 특정 값 삭제</h1> <%session.removeAttribute("id");%> <h3>id : <%=session.getAttribute("id")%></h3> <h3>pwd : <%=session.getAttribute("pwd") %></h3> <h3>name : <%=session.getAttribute("name") %></h3> <h1>전체 세션 만료</h1> <%session.invalidate();%><!-- 강제로 세션 만료시킴. 새로고침 후 확인 --> <!-- session 객체 자체를 null로 만들기 때문에 예외발생 <h3>id : <%=session.getAttribute("id")%></h3> <h3>pwd : <%=session.getAttribute("pwd") %></h3> <h3>name : <%=session.getAttribute("name") %></h3> --> </body> </html> | cs |
session.invaliate()를 이용하면 전체 세션을 만료시킬 수 있다.
'JSP > 기본다지기' 카테고리의 다른 글
JSP 7일차 필기 (Cookie) (0) | 2016.10.26 |
---|---|
JSP 6일차 필기 (Session을 이용한 로그인) (0) | 2016.10.25 |
JSP 6일차 필기 (Template) (0) | 2016.10.25 |
JSP 5일차 필기 (Template) (0) | 2016.10.24 |
JSP 5일차 필기 (Dispatcher) (0) | 2016.10.24 |