티스토리 뷰

FRONT-END/JSP

[JSP] ATTRIBUTE & SCOPE - 속성과 영역

진심스테이크 2018. 4. 12. 21:50

 

Attribute (속성) : 공유되는 데이터

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

 

 

속성 처리 메소드

 

 메소드

리턴 타입 

설명 

setAttribute(String name, Object Value)

void 

이름이 name인 속성 값을  value로 지정 

getAttribute(String name) 

Object 

이름이 name인 속성의 값을 구함

#지정한 이름이 속성에 존재하지 않으면 null리턴 

 removeAttribute(String name)

void 

이름이 name인 속성을 삭제 

getAttributeNames( ) 

Enumeration<String> 

속성의 이름 목록을 구함

#page에서는 메소드 제공 안함 

 

 

영역

 

1. PAGE

- 하나의 JSP 페이지를 처리 -> 현재 페이지까지

- 한 번의 요청을 처리하는 하나의 JSP 페이지 내에서 공유할 값을 저장

- 주로 커스텀 태그에서 새로운 변수를 추가할 때 사용

- 영역 객체 : pageContext

 

2. REQUEST

- 하나의 HTTP 요청을 처리 ->

- 한 번의 요청을 처리하는 데 사용되는 모든 JSP 페이지에서 공유할 값 저장

- 주로 하나의 요청을 처리하는 데 사용하는 JSP 페이지 사이에서 정보를 전달하기 위해 사용

- 영역 객체 : request

 

3. SESSION

- 하나의 웹 브라우저와 관련

- 한 사용자와 관련된 정보를  JSP 사이에 공유하기 위해 사용

- 사용자의 로그인 정보 등등

- 영역 객체 : session 

 

4. APPLICATION

- 하나의 웹 어플리케이션과 관련

- 모든 사용자를 위해 공유할 정보 저장

- 임시 폴더 경로와 같이 웹 어플리케이션의 설정 정보를 주로 저장

- 영역 객체 : application

 

 


 

예시

- 4개의 JSP 파일을 사용

 

page1.jsp

<%@ 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>

 

 

page2.jsp

<%@ 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>
 

page3.jsp

<%@ 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>

 

 

page4.jsp

<%@ 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>

 


 

실행 결과

 

 

'FRONT-END > JSP' 카테고리의 다른 글

[JSP] 간단한 장바구니  (0) 2018.04.13
[JSP] SENDREDIRECT( )  (1) 2018.04.13
[JSP] 오류 예외처리  (0) 2018.04.12
[JSP] 한글 처리  (0) 2018.04.12
[JSP] 내장 객체  (1) 2018.04.12
댓글