티스토리 뷰

 

 

코드 설명


store.jsp 에서 세션을 저장하고

remover.jsp 에서 세션을 삭제하여

session_send에서 세션을 출력한다.

 


session_init.jsp 에서 세션을 초기화하고 

session_send에서 세션을 출력한다.

 

 

 

 

session_send.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

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%=session.getId()%>

<%@ page import="java.lang.String"%>

<%

    String name;

    //session 객체의 getAttribute 메서드로 name 속성이 존재하는지 확인한다.

    if (session.getAttribute("name"!= null) {

        //session 객체의 getAttribute 메서드로 name 속성을 name 변수에 String 클래스 타입으로 강제 타입 변환 후 할당한다.

        name = (String) session.getAttribute("name");

    } else {

        name = "없습니다.";

    }

%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

    <br/>

    <br/>

    <input type="button" onclick="location.href='./store.jsp'" value="저장">

    <input type="button" onclick="location.href='./remover.jsp'" value="삭제">

    <input type="button" onclick="location.href='./session_init.jsp'" value="초기화"> 

    <br/>

    <br/>

    세션 값: <%=name %>

</body>

</html>

Colored by Color Scripter

cs




 

 

 

 

 

store.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"%>

<!--     session 객체의 setAttribute 메서드로 세션 객체에 "Jae"이라는 값이 들어 있는 name 속성을 저장한다 -->

<%session.setAttribute("name", "Jae"); %>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<br/>

<%session.setAttribute("name", "홍길동"); %>

<script type="text/javascript">

//자바스크립트의 location 객체의 href 속성으로 session_send.jsp로 이동한다.

location.href="./session_send.jsp";

 

 

</script>

 

</body>

</html>

Colored by Color Scripter

cs

 

 

 

remover.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"%>

<!--     session 객체의 removeAttribute 메서드로 존재하는 name 속성을 제거한다. -->

<!-- ⎼removeAttribute 메서드는 세션에 저장해둔 속성을 이미 다 사용하였고 더 이상 사용할 필요가 없을 때 제거하게 된다. -->

<% session.removeAttribute("name"); %>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

    <script type="text/javascript">

//     자바스크립트의 location 객체의 href 속성으로 session_send.jsp로 이동한다.

    location.href = "./session_send.jsp"

    </script>

 

</body>

</html>

Colored by Color Scripter

cs

 

 

 

session_init.jsp


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<% session.invalidate(); %>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<script type="text/javascript">

    location.href="./session_send.jsp";

</script>

 

 

</body>

</html>

Colored by Color Scripter

cs

 

 

 

 

출력 화면


 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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
글 보관함