2019.01.31 Session의 이해

2019. 1. 31. 16:46JSP

① sessionTest.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <!-- 
        세션(Session): 클라이언트 당 유지되는 접속정보를 서버 메모리에 저장 
                                       중요한 정보 (로그인 인증,개인정보 저장)에 사용  
        -세션값 생성 -> session.setAttribute("이름","값"); 
        -세션값 가져오기 -> session.getAttribute("이름");
        -세션 값 한개 삭제 -> session.removeAttribute("이름");
        -세션  초기화 -> session.invaliate();
        
        쿠키: 클라이언트에 파일로 저장 , 서버에 부하를 주지않고 값을 저장
            (자동로그인여부,광고관련 데이터 저장)
    -->
    <% //세션값 가져오기
        String name = (String)session.getAttribute("name");
        Integer age = (Integer)session.getAttribute("age");
    %>
    
    
    <h1>sessionTest.jsp</h1>
    <h3>세션값 이름 : <%=name %> </h3>
    <h3>세션값 나이 : <%=age %> </h3>
    <button type ="button" onclick="location.href='sessionSet.jsp?name=박보검&age=27'">세션값 저장</button>
    <button type ="button" onclick="location.href='sessionDel.jsp'">세센값 삭제</button>
    <button type ="button" onclick="location.href='sessionInval.jsp'">세센 초기화</button>
    
</body>
</html>
cs



②sessionSet.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 charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <% 
        String name = request.getParameter("name");
        String strAge = request.getParameter("age");
        int age = Integer.parseInt(strAge); //"33"
        
        //세션값 생성
        session.setAttribute("name",name);
        session.setAttribute("age",age); // 오토박싱
    %>
    <script>
    alert('세션값이 생성됨');
    location.href='sessionTest.jsp'//경로이동
    
    
    </script>
</body>
</html>
cs


③sessionDel.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 charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <% //session삭제
        session.removeAttribute("name");
    %>
    <script>
        alert('세션값 name이 한개가 삭제됨');
        location.href='sessionTest.jsp';
    </script>
</body>
</html>
cs


sessionInval.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 charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <% //session 초기화
        session.invalidate();
    %>
    <script>
        alert('세션이 초기화됨');
        location.href='sessionTest.jsp';
    </script>
</body>
</html>
cs



실행결과



'JSP' 카테고리의 다른 글

2019.02.01 Cookie 만들기(2)  (0) 2019.02.01
2019.01.31 Session의 활용 : 로그인페이지  (0) 2019.01.31
2019.01.31 get형식 & post형식  (0) 2019.01.31
2019.01.30 Cookie 만들기(1)  (0) 2019.01.30
2019.01.30 JSTL 라이브러리  (0) 2019.01.30