본문으로 바로가기

[JSP][jsp, 스코프 예제]

category JSP 2018. 4. 13. 01:43

 

JSP

 

 

 속성(Attribute)과 영역(scope ) 개념정리 및 실습

 

속성 (Attribute) : 공유되는 데이터
영역 (Scope) : 속성을 공유할수있는 유효범위.
영역객체 : 해당 영역에서 속성을 사용할수있는 내장 객체들을 영역개체라고 말함

 

1) page scope =

page 영역은 위에 3가지 영역과는 다르게 page  내장객체가아닌 pageContest 내장 객체를 통해 접근 할수 있는 영역이라는 것 입니다.

 

2)  request scope =

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

즉 forward또는 include방식을 이용하는 경우 여러개의 페이 지에서도 요청정보가 계속 유지 되므로request영역의 속성을 여러 페이지에서 공유 할수 있습니다.


3) session scope =

session내장 객체는 세션이 유지되고 있는  범위안에서 즉,session scope안에서

서로 다른 페이지(자원)이라고 할지라도 객체(데이터)들을  공유할수있는 속성을 

 가지고 있으며 이 속성에 내장된 객체(데이터)는 세션이 종료되는 순간에 변환된다.

  

4) application scope =

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

즉,쉽게 말하자면 학원 게시판에 빗대어 이야기 해보겠다.

게시판은 모든이가  볼수가 있습니다.따라서 application은 모든 이가 공유 할수 있는 데이터입니다.->가장 큰 영역 

 

 

진짜 쉽게 말하자면  영역이 큰 순서

page scope=현재 페이지 에서만 

 request scope =한페이지만 넘겨 받을 수있다

session scope =브라우저,즉 크롬이면 크롬만 ,Explorer에 넘겨줄수없다

application scope =물불안가린다 어디서까지든 상관없다. 

 

밑에는 위네가지 스코프들을 이용한 예제이다 pege는 따로 만들어 주지 않았다

지금 까지 설명한 개념을 확실하게 파악하고 나서  코드를 보기 바란다.

 

-값이 전달되지 않을 겨우에는 "null"나오게된다

 ???가나온다는 것은  한글을 인식하지 못하는 것이니  당황하지 말고 주석에 보면  한글을 위한 코드라 되어있는 코드를 입력 해준다.

 

 

 

 

1.page1.jsp

=Application 영역으로 지정할 name과 id를 만듬

 

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

 

 

 

 

 

2.page2.jsp

= request.getParameter( )를  name 으로 초기화 후 application 영역으로 잡아주므로 어디서든 다쓰인다

 

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
55
56
57
58
59
60
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    request.setCharacterEncoding("euc-kr"); 
//<% 스크립트를 사용 했을 때 스크립트 영역에서는 JAVA코딩이 가능 하다 
//요청한다.request객체를 통해 넘어온  파라미터중에서 한글이 있기때문에 한글리를 위한 코딩
%>
<!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);//String타입 변수 name과 id에다가  요청받은 파라미터 name과 id정장
        //application영역에setAttribute()메소드를 사용하므로 어느페이지에서라도 anme과 id를 사용할수있다.
        //name값의 이름 으로 name객체를 등록
%>
<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 영역 : 어느 페이지에서 사용 가능 -->
<!--요청한 name과id를 이용함  -->
    <h3 align="center"><%=name%>입니다.<br><%=name%>님의 아이뒤는
        <%=id%>입니다.
    </h3>
        <!--이것은 어느페이지 에서도 사용가능 application영역이여서   -->
    <!--form태그를 사용하므로 attributeTest2.jsp 파일로 post방식으로 파라미터들을 넘겨주는데 
그파라미터들의 내용은 table태그이면서 session영역에 저장할  데이터들을 입력 받을 form을 생성한것  
    page3.jsp에 보면 session이 만들어 져있다 -->
    <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

 

 

 

 

3.page3.js

= page2.jsp session이용

 

 

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 영역 등록된 속성들  setAttribute(String name, Object Value)
    session.setAttribute("mail", request.getParameter("mail"));//page2.jsp에서 만들어준 것을session영역으로 셋한다.
    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

 

 

 

 

 

 

 

 

 

4.page4.jsp

=application 이용

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
<%@ 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><!-- //application이기때문에  영역에 제한없이 받아온다  -->
            </tr>
            <tr>
                <td align="center">아이디</td>
                <td><%=application.getAttribute("id")%></td><!-- //application이기때문에  영역에 제한없이 받아온다  -->
            </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><!--session은 다른 블라우져만 아니라면 괜찮기기 때문에 상관없다   -->
            </tr>
            <tr>
                <td>주소</td>
                <td><%=session.getAttribute("address")%></td><!--session은 다른 블라우져만 아니라면 괜찮기기 때문에 상관없다   -->
            </tr>
            <tr>
                <td>전화번호</td>
                <td><%=session.getAttribute("tel")%></td><!--session은 다른 블라우져만 아니라면 괜찮기기 때문에 상관없다   -->
            </tr>
        </table>
    </form>
</body>
</html>
cs

 

 

 

 

 

 

실행시킬경우!!!

첫페이지

1.page1.jsp

 

 

2.page2.jsp

 

 

 

 

 

3.page3.jsp

 

4.page4.jsp

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'JSP' 카테고리의 다른 글

[JSP][에러 페이지만들기]  (0) 2018.04.14
[JSP][장바구니 만들기]  (0) 2018.04.13
[JSP][한글 깨짐 처리]  (0) 2018.04.12
MVC  (0) 2018.04.11
에러 다잠아야지 !!  (0) 2018.04.11