본문 바로가기

프로그래밍/Java

Forward && Redirect

02.jsp 

<?xml version="1.0" encoding="EUC-KR" ?>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<% String cp = request.getContextPath(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR" />
<title>Insert title here</title>
</head>
<body>
<%
 //창고 3종 셋트당~~ 우선순위 
      pageContext.setAttribute("name", "어렵다네~");  //page가 넘어가면 죽는다~
      request.setAttribute("name", "안와도영");           //forward 일 경우 request는 살아 있다.
      session.setAttribute("name", "최반맛간");
      application.setAttribute("name", "더위영태");
%>
<h1>${name }</h1>

// ↓ 이것을 사용하게 되면 request가 살아 있게 된다.
<%-- <jsp:forward page="03.jsp" />--%>
<%

     // ↓ 이것을 사용하게 되면 위에 있는 <h1> ${name } </h1> 무시하고 바로 03.jsp 이동한다

     // redirect 사용시 request 도 같이 죽는다!!!!
     response.sendRedirect("03.jsp");
%>
</body>
</html> 

 

03.jsp 

<?xml version="1.0" encoding="EUC-KR" ?>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<% String cp = request.getContextPath(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR" />
<title>03.jsp입니다~~</title>
</head>
<body>
<h1>03.jsp입니다~~~~~~~~~~~~~</h1>
<h1>${name }</h1>
<h1>${sessionScope.name }</h1>
<h1>${applicationScope.name }</h1>
</body>
</html> 

 

04.jsp 

<?xml version="1.0" encoding="EUC-KR" ?>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<% String cp = request.getContextPath(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR" />
<title>Insert title here</title>
</head>
<body>

   // ↓ jstl 입니다 redirect와 같은 효과
  <c:redirect url="http://localhost:8080/pp/jstl/03.jsp"/>
</body>
</html>

 

 

★ 부가설명

 

Forward 를 이용할 경우 request 의 값이 다음페이지에 가서도 유지가 되고

Redirect를 사용할 경우 현재 있는 page에 대한 <h1l> 값은 실행하지 않고 바로 이동해서 실행한다.

 

 

 

'프로그래밍 > Java' 카테고리의 다른 글

contains + split 기능 사용하기  (0) 2013.07.19
Hash Map 사용하기  (0) 2012.12.16
JSTL 설정 방법  (0) 2012.07.31
HanServlet 실습  (0) 2012.07.26
DdayServlet 실습  (0) 2012.07.26