본문 바로가기
Web Programming/JSP

[JSP] 영역(Scope)과 속성(Attribute)

영역(Scope)과 속성(Attribute)







속성/Attribute : 공유되는 데이터

영역/Scope : 속성을 공유 할 수 있는 유효범위


Session

세션이 유지되고 있는 범위 안에서 session scope 안에서 서로 다른 페이지라고 할지라도 객체(데이터)들을 공유 할 수 있는 속성을 가지고 있으며 이 속성에 내장된 객체는 세션이 종료되는 순간에 반환 된다.

다른 브라우저 작업시, 휘발

한 브라우저 내에 1개의 session만 생성


Request

request 내장 객체는 클라이언트의 요청이 처리 되는 동안 속성을 사용 할 수 있다.

즉, 포워딩 또는 인클루드 방식을 이용하는 경우 여러개의 페이지에서도 요청 정보가 계속 유지 되므로, request 영억의 속성을 여러 페이지에서 공유 할 수 가 있다.


Application

웹 어플리케이션이 실행되고 있는 동안 속성을 사용 할 수가 있다.

application은 모든이가 공유 할 수 있는 데이터 이다.


Page

page 영역은 위의 3가지 영역과 다르게 page 내장 객체가 아닌 pageContext 내장 객체를 통해 접근 할 수 있는 영역이다.

pageContext는 모든 영역의 속성에 대한 접근이 가능하다.







Page1.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
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    request.setCharacterEncoding("euc-kr"); //한글처리
%>
<!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=EUC-KR">
<title>Page.1</title>
</head>
<body>
    <h2 align="center">Page.1</h2>
    <!-- Application 영역 -->
    <center>
        <form name="form1" method="post" action="page2.jsp">
            <table border="1" align="center">
                <tr>
                    <td align="center">이름</td>
                    <td><input type="text" name="name" /></td>
                </tr>
                <tr>
                    <td align="center">아이디</td>
                    <td><input type="text" name="id" /></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input type="submit" value="확인" />
                    </td>
                </tr>
            </table>
        </form>
    </center>
</body>
</html>
cs



Page2.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    request.setCharacterEncoding("euc-kr"); //한글처리
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
    //String 객체에 요청 받은 파라미터 값 저장 : request
    //밑에 name, id를 쓰기 위해 선언
    String name = request.getParameter("name");
    String id = request.getParameter("id");
 
    //정보 저장 : application
    application.setAttribute("name", name);
    application.setAttribute("id", id);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Page.2</title>
</head>
<body>
    <h2 align="center">Page.2</h2>
    <!-- application 영역 : 어느 페이지에서 사용 가능 -->
    <h3 align="center"><%=name%>
        반갑수다 <br> 아듸는
        <%=id%>
        -- 이거여
    </h3>
    <form action="page3.jsp" method="post">
        <table align="center" border="1">
            <tr>
                <td align="center" colspan="2">PAGE.2</td>
            </tr>
            <tr>
                <td align="center">이메일</td>
                <td><input type="text" name="mail" /></td>
            </tr>
            <tr>
                <td align="center">주소</td>
                <td><input type="text" name="address" /></td>
            </tr>
            <tr>
                <td align="center">전화번호</td>
                <td><input type="text" name="tel" /></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="확인" /></td>
            </tr>
        </table>
    </form>
</body>
</html>
 
cs



Page3.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=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    request.setCharacterEncoding("euc-kr"); //한글처리
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
    //정보 저장 : session
    session.setAttribute("mail", request.getParameter("mail"));
    session.setAttribute("address", request.getParameter("address"));
    session.setAttribute("tel", request.getParameter("tel"));
 
    //밑에 name을 쓰기 위해 선언
    String name = (String) application.getAttribute("name");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Page.3</title>
</head>
<body>
    <h2 align="center">Page.3</h2>
    <!-- session 영역 -->
    <form action="page4.jsp">
        <h3 align="center">
            <%=name%>
            정보 저장 ☆완-료☆
        </h3>
        <br>
        <h3 align="center">
            <input type="submit" value="확인">
        </h3>
    </form>
</body>
</html>
cs



Page4.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    request.setCharacterEncoding("euc-kr"); //한글처리
%>
<%@ page session="true"%>
<!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=EUC-KR">
<title>Page.4</title>
</head>
<body>
    <h2 align="center">Page.4</h2>
    <form method="post">
        <table border="1" align="center">
            <tr>
                <td colspan="2" align="center">Page.1</td>
            </tr>
            <tr>
                <td align="center">이름</td>
                <td><%=application.getAttribute("name")%></td>
            </tr>
            <tr>
                <td align="center">아이디</td>
                <td><%=application.getAttribute("id")%></td>
            </tr>
        </table>
    </form>
    <form method="post">
        <table border="1" align="center">
            <tr>
                <td colspan="2" align="center">Page.2</td>
            </tr>
            <tr>
                <td>이메일</td>
                <td><%=session.getAttribute("mail")%></td>
            </tr>
            <tr>
                <td>주소</td>
                <td><%=session.getAttribute("address")%></td>
            </tr>
            <tr>
                <td>전화번호</td>
                <td><%=session.getAttribute("tel")%></td>
            </tr>
        </table>
    </form>
</body>
</html>
 
cs