본문 바로가기

DATA 분석 교육 과정 (2024.02~08)/JSP&Servlet

JSP_Forward, sendRedirect, Scope

728x90

 

 

 

 

<Ex01_Scope.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 : 객체의 범위, 유효기간
	//1. page: 하나의 jsp페이지 내( page는 오직 하나의 jsp페이지만을 포함/)
	//2. request : 하나의 요청에 대해서 (하나의 요청을 처리하는데 사용되는 모든 jsp페이지 포함)
	//   request 영역에 저장된 정보를 얻기 위해 request객체가 필요, 입력폼엣 사용자가 입력한 값을 
	//   다음 페이지를 처리해야 하는데 페이지 이동이 있은 후에는 이전 페이지의 상태가 유지되지 않는다.
	//   하지만 request 이전 페이지에서 입력된 값(파라미터)를 getparameter 
	//   또는 get//Atrtibute 메소드를 사용하여 얻어올수 있다. 
	//3. session : 하나의 브라우저의 대해서
	//4. application :  하나의 웹 어플리케이션에 대해서
	
	//생성: 객체.setAttribute("이름",값:object)
	pageContext.setAttribute("page","pageOK");
	request.setAttribute("request", "requestOK");
	session.setAttribute("session", "sessionOK");
	application.setAttribute ("application","applicationOK");
	
	//조회 : 객체.getAttribute("이름")
	// pageConte//xt.getAttribute("page");>> object에서 형변환
	String getPage =(String)pageContext.getAttribute("page");
	String getRequest =(String)request.getAttribute("request");
	String getSession =(String)session.getAttribute("session");
	String getAppl =(String)application.getAttribute("application");
	
	// forward 실행해보기
	// RequestDispatcher : request객체를 이동할 페이지로 넘겨주는 역할을 하는 객체
	RequestDispatcher rd = request.getRequestDispatcher("./Ex02_Scope.jsp");
	rd.forward(request,response);
%>
<h1> Ex01_scope </h1>
page : <%=getPage %> <br>
Request : <%=getRequest %> <br>
Session : <%=getSession %> <br>
application : <%=getAppl %> <br>

</body>
</html>

 

 

<Ex02_Scope.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>


<%
	//조회 : 객체.getAttribute("이름")
	String getPage =(String)pageContext.getAttribute("page");
	String getRequest =(String)request.getAttribute("request");
	String getSession =(String)session.getAttribute("session");
	String getAppl =(String)application.getAttribute("application");

%>

<h1> Ex02_scope </h1>
page : <%=getPage %> <br>
Request : <%=getRequest %> <br>
Session : <%=getSession %> <br>
application : <%=getAppl %> <br>

</body>
</html>

 

 

 

 

출처: 스마트인재개발원

728x90
반응형

'DATA 분석 교육 과정 (2024.02~08) > JSP&Servlet' 카테고리의 다른 글

Mybatis/Lombok (실습)_Login  (0) 2024.04.18
JSP_Forward (실습)  (0) 2024.04.16
JSP_Session  (0) 2024.04.16
JSP_(실습)다른페이지로 이동하기  (0) 2024.04.15
JSP_다른페이지로 이동하기  (0) 2024.04.15