[JSP] ch03-06. Scope

2017. 11. 14. 11:25·WEB/JSP
728x90
반응형

3-6. Scope

scopeMain.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<a href="scopeProc.jsp?way=redirect">redirect</a>
<a href="scopeProc.jsp?way=forward">forward</a>
 

 

scopeProc.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.net.URLEncoder"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%
    pageContext.setAttribute("scope", "페이지"); //redirect: 페이지 넘어가면 값이 사라짐, forward: 페이지 넘어가면 값이 사라짐 
    request.setAttribute("scope", "리퀘스트"); //redirect: 페이지 넘어가면 값이 사라짐., forward: 값이 살아있음
    session.setAttribute("scope", "세션"); //둘다 살아있음
    application.setAttribute("scope", "애플리케이션"); //둘다 살아있음
    
    String way = request.getParameter("way");
    
    switch(way){
    case "redirect": //처리를 한번만 할때
        String userName = URLEncoder.encode("최한석","utf-8");
        response.sendRedirect("scopeOut.jsp?userName="+userName); break;
    case "forward" : //처러를 여러번해도 상관없을 때
%>
        <jsp:forward page="scopeOut.jsp"/>
<%
    }
%>
<%--
pageContext.setAttribute("scope", "페이지");
request.setAttribute("scope", "리퀘스트");
session.setAttribute("scope", "세션");
application.setAttribute("scope", "애플리케이션");

Redirect(세션, 애플리케이션)
Forward(리퀘스트, 세션, 애플리케이션)

scope(스코프): 접근범위
: page - 하나의 페이지에서만 적용 (디폴트) (현재페이지가 실행되는동안)
: request - 두개의 페이지에서 적용 (response가 클라이언트까지 갈때까지)
: session - 세션이 유지되는 동안 적용 (논리적인 연결이 유지되는 동안)
                     / 로그인 동안 ex) 쇼핑몰 장바구니
                     / 세션타임 동안 (기본 30분) ex) 은행 자동 로그아웃
                     / 하나의 브라우저에서 : 브라우저가 다를 경우는 새로운 세션
: application - 서버가 유지되는 동안 적용 (현 애플리케이션이 실행되는 동안)

여러페이지에서 꺼내 써야할 때 request. session
페이지 이동 순서를 예측할 수 있다. request.
페이지 이동 순서를 예측할 수 없다. session.
--%>

 

scopeOut.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
    String pageScope = (String)pageContext.getAttribute("scope");
    String requestScope = (String)request.getAttribute("scope");
    String sessionScope = (String)session.getAttribute("scope");
    String applicationScope = (String)application.getAttribute("scope");
    
    String way = request.getParameter("way");
    String userName = request.getParameter("userName");
%>
<ul>    
    <li><b>page:</b>                <%= pageScope%></li>
    <li><b>request:</b>             <%= requestScope%></li>
    <li><b>session:</b>             <%= sessionScope%></li>
    <li><b>application:</b>         <%= applicationScope%>
    <li><b>parameter(way):</b>      <%= way%></li>
    <li><b>parameter(userName):</b> <%= userName%></li>
</ul>
 

728x90
반응형

'WEB > JSP' 카테고리의 다른 글

[JSP] ch04-01. Cookie  (0) 2017.11.15
[JSP] ch03-07. Include3  (0) 2017.11.14
[JSP] ch03-05. Forward Login  (0) 2017.11.13
[JSP] ch03-04. Redirect Login  (0) 2017.11.13
[JSP] ch03-02. Request  (0) 2017.11.13
'WEB/JSP' 카테고리의 다른 글
  • [JSP] ch04-01. Cookie
  • [JSP] ch03-07. Include3
  • [JSP] ch03-05. Forward Login
  • [JSP] ch03-04. Redirect Login
밍글링글링
밍글링글링
mingling - 밍글링, 밍글밍글링. 코드와 어우러지다. IT/ 프로그래밍/소스
    반응형
    250x250
  • 밍글링글링
    mingling
    밍글링글링
  • 전체
    오늘
    어제
    • 밍글링글링 (407) N
      • Flutter (2)
      • 일상생활 (8)
        • 리뷰 (1)
        • 생활정보 (4)
        • 맛집 (0)
        • 여행 (0)
        • 모든정보 (3)
      • JAVA (126)
        • 개념 (6)
        • 예제 (115)
        • Exception (2)
      • C (1)
        • C (1)
        • C++ (0)
        • C# (0)
      • JS (29)
        • JavaScript (18)
        • JQuery (5)
        • AJax (0)
        • NODE.JS (6)
        • Angular.JS 2.0 (0)
      • WEB (87)
        • HTML (6)
        • CSS (61)
        • JSP (20)
        • JSTL (0)
      • FrameWork (8)
        • Spring (8)
        • BootStrap (0)
        • MyBATIS (0)
        • JUnit (0)
      • 외부 라이브러리 (5)
      • 공유 소스 관리 (5)
        • Git (5)
        • SVN (0)
      • 빅데이터 프로그래밍 (37)
        • Python (37)
        • R Programming (0)
      • DB (7)
        • ORACLE (0)
        • MySql (6)
      • Development Tools (7)
        • StarUML (0)
        • eXERD (0)
        • Eclipse (4)
      • SKILL (6)
        • Migration (0)
        • Security (6)
      • MicroSoft (0)
        • Excel (0)
        • Word (0)
      • Android (0)
      • Server (21)
        • Ubuntu (5)
        • Linux (15)
      • IOS (0)
      • XML (0)
      • 미디어 (0)
      • 공지사항 (3)
      • NETWORK (1)
      • 게임 (4)
        • 피파 (1)
        • 리니지M (0)
        • 배틀그라운드 (1)
        • 듀랑고 (2)
      • 세상 이슈 (6)
      • 일렉트론 (0)
      • 대회 소식 (4)
      • 업무 (2)
      • Express, Vue (6)
      • docker (11)
      • svelte (3)
      • 블록체인 (1)
      • IT (10) N
      • Rust (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 미디어로그
    • 위치로그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    mysql db
    rust linux
    Node
    자바 배열
    gitlab 설치
    브라우저 자동화
    vue 설치
    API 설계
    nginx ssl 설정
    프런트엔드
    vue cli
    React Compiler
    ssl 인증서 발급
    ubuntu
    Extension Bisect
    자바 객체 지향
    자바 클래스
    lang rust
    servlet class
    css block
    css table
    java casting
    jsp parameter
    자바 for문
    SSL 인증서
    jsp include
    에디터 팁
    css transition
    오류
    css list
    자바 exception
    css perspective
    Rust lang
    proxy pass
    러스트
    AI 코딩 에이전트
    nginx ssl 적용
    리눅스 설치
    Java Array
    vscode
    spring java
    css tb
    css float
    docker
    nginx
    티스토리 자동화
    VS Code 팁
    클론코딩
    자바 생성자
    svelte
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
밍글링글링
[JSP] ch03-06. Scope
상단으로

티스토리툴바