<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
// 각각의 Scope에 데이터 저장하기
// Page Scope : 하나의 jsp페이지 안에서만 유지(pageContext)
pageContext.setAttribute("page", "Page Scope 입니다.");
// Request Scope : 한번의 요청-응답이 이루어지는 동안(request)
request.setAttribute("req", "Request Scope 입니다.");
// Session Scope : 하나의 브라우저 내에서 유지(session)
session.setAttribute("ses", "Session Scope 입니다.");
// Application Scope : 프로그램 동작하는 동안 유지(application)
application.setAttribute("app", "Application Scope 입니다.");
%>
<%
// Scope에서 값 꺼내기
// 불러온 데이터를 가져올때는 다운캐스팅 해야함
String page1 = (String)pageContext.getAttribute("page");
String req = (String)request.getAttribute("req");
String ses = (String)session.getAttribute("ses");
String app = (String)application.getAttribute("app");
out.print(page1 + "<br>");
out.print(req + "<br>");
out.print(ses + "<br>");
out.print(app + "<br>");
%>
<a href="ex11select.jsp">스코프 확인</a>
</body>
</html>
파일 실행시 4개 전부 나옴
// 페이지 이동
response.sendRedirect("ex11select.jsp");
ex11select.jsp 페이지로 이동 후 Page Scope 와 Request Scope는 null이 됨
클라이언트가 서버에 요청을 한뒤 sendRedirect로 보내면 다시 클라이언트가 서버에 요청하게 되기 때문에 req는 null이 됨
// forwoard 방식으로 페이지 이동
// 1. 이동을 도와줄 RequestDispatcher 객체 생성
// request.getRequestDispatcher("이동할 페이지 주소 or URLMapping");
// -> 프로젝트 내 파일로만 이동가능
RequestDispatcher rd = request.getRequestDispatcher("ex11select.jsp");
// 2. forward 이동
rd.forward(request, response);
// 요청시에 주소값이 바뀌기 때문에 sendRedirect는 ex11select.jsp로 바뀌지만 forward로 이동할땐 ex11scope.jsp 주소 그대로 가짐
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
// Scope에서 값 꺼내기
// 불러온 데이터를 가져올때는 다운캐스팅 해야함
String page1 = (String)pageContext.getAttribute("page");
String req = (String)request.getAttribute("req");
String ses = (String)session.getAttribute("ses");
String app = (String)application.getAttribute("app");
out.print(page1 + "<br>");
out.print(req + "<br>");
out.print(ses + "<br>");
out.print(app + "<br>");
%>
</body>
</html>
스코프 조회
스코프 조회 결과화면
'Cookie&Session' 카테고리의 다른 글
[Session] 로그아웃 (0) | 2022.05.16 |
---|---|
[Session] 로그인 & 로그인 실패 (0) | 2022.05.16 |
[Session] 세션 전체 삭제 (0) | 2022.05.16 |
[Session] 세션 삭제 (0) | 2022.05.16 |
[Session] 세션 수정 (0) | 2022.05.16 |