L06 세션으로 값 전달(1/2)
2016. 10. 27. 16:38ㆍProgramming/JSP
**배운 것**
1) 세션: 쿠키와 달리 서버상 객체로 존재.
그 덕분에 보안이 좋고, 저장 데이터 한계 없음
<세션 생성 과정>
2) 세션 메소드
getAttribute() : 세션에서 데이터를 get(<-> setAttribute())
getAttributeNames() : 세션에 저장되어 있는 모든 데이터의 이름(유니크한 키값)을 get
getId() : 자동 생성된 세션의 유니크한 아이디를 get
isNew() : 세션이 최초 생성되었는지, 이전에 생성된 세션인지를 구분
getMaxInactiveInterval() : 세션의 유효시간을 get. 가장 최근 요청시점을 기준으로 카운트 됩니다.
removeAttribute() : 세션에서 특정 데이터 제거
Invalidate() : 세션의 모든 데이터를 삭제
<index.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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<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 |
<1번 눌렀을 때:L01SetSession.jsp>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 |
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<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는 모두 오브젝트
session.setAttribute("pw", 1234);
session.setAttribute("name", "ckm");
out.print("<h3>설정 완료</h3>");
%>
<p><a href="javascript:history.go(-1)">뒤로</a></p>
</body>
</html>
|
cs |
<2번 눌렀을 때:L02GetSession.jsp>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 |
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>세션 값 얻어오기</title>
</head>
<body>
<h1>세션 값</h1>
<!-- 세션은 서버 종료시에도 유지됨, 단 브라우저 종료시 사라짐 -->
<h3>id : <%=session.getAttribute("id") %></h3>
<h3>pw : <%=session.getAttribute("pw") %></h3>
<h3>name : <%=session.getAttribute("name") %></h3>
<p><a href="javascript:history.go(-1)">뒤로 가기</a></p>
</body>
</html>
|
cs |
<3번 눌렀을 때: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
25
26
27 |
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import = "java.util.Enumeration"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<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:history.go(-1)">뒤로 가기</a></p>
</body>
</html>
|
cs |
<4번 눌렀을 때: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
27
28
29
30
31
32
33
34
35
36
37
38
39
40 |
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>세션이 가진 정보를 보자</title>
</head>
<body>
<h1>세션 정보</h1>
<h3>1. 세션 아이디 : <%=session.getId()%></h3>
<!-- 서버가 브라우저(클라이언트) 구분하기 위해 붙이는 고유 ID
브라우저 접속시마다 생성
서버가 종료될 때 or 강제 만료될 때까지 유지됨
-->
<h3>2. 세션 생성 여부 : <%=session.isNew()%></h3>
<!-- 브라우저 최초 접속시만 true를 반환,
세션은 page에 생성되는 게 아니므로 프로젝트 내부에서 같은 세션이 공유됨 -->
<%
/* 전체 세션 만료시간 설정
web.xml에 session-timeout 지정하기
<session-config>
<session-timeout>분</session-timeout> 지정하기
</session-config>
*/
//특정 세션 만료시간 설정(n분*60)
session.setMaxInactiveInterval(5*60);
long lastTime = session.getLastAccessedTime();
long createTime = session.getCreationTime();
long usedTime = (lastTime - createTime)/60000; //분
long inactive = session.getMaxInactiveInterval()/60; //분
%>
<h3>3. 세션 유효 시간 : <%=inactive%></h3>
<!--페이지를 방문한 시점으로부터 유효시간:새로고침할때마다 30분으로 초기화-->
<h3>4. 웹사이트에 머문 시간 : <%=usedTime%></h3>
</body>
</html>
|
cs |
<5번 눌렀을 때:L04InfoSession.jsp>
***id만 삭제
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 |
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<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>pw : <%=session.getAttribute("pw") %></h3>
<h3>name : <%=session.getAttribute("name") %></h3>
<h1>전체 세션 만료</h1>
<%session.invalidate();%> <-- 세션을 만료시킴 -->
</body>
</html>
|
cs |
'Programming > JSP' 카테고리의 다른 글
L07 쿠키로 값 전달 (0) | 2016.10.27 |
---|---|
L06 세션으로 값 전달(2/2) ~로그인/로그아웃 (0) | 2016.10.27 |
L05 Template (0) | 2016.10.26 |
L04 Redirect(페이지 넘기기) (0) | 2016.10.26 |
L03 서블릿 메소드(2/2) (0) | 2016.10.25 |