본문 바로가기
WEB Archive

[HTML] JSP - Redirect 실습

by universedevelope 2024. 8. 21.

Send10.jsp ▼▼▼

<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Send10.jsp</title>
</head>
<body>


<div>
	<h1>데이터 송수신 실습10</h1>
	<hr>
</div>
<div>
<!-- Send10.jsp → Send10_re.jsp → Receive10.jsp	
Send10.jsp → Send10_for.jsp → Receive10.jsp -->

<!-- 사용자 최초 요청 페이지 -->	
<!-- http://localhost:8090/WebApp07/Send10.jsp -->

</div>

<div>
	<h2>포워딩 및 리다이렉트</h2>
</div>

<div>
	<form action="" method="post">
		이름<input type="text" name="userName" class="txt">
		<br>
		<br>
		
		<!-- check~~ -->
		<button type="submit" class="btn" style="width: 150px"
		onclick="this.form.action='Send10_re.jsp'">리다이렉트</button>
		<!-- 1번째 시도 : null / null -->		
		<button type="submit" class="btn" style="width: 150px"
		onclick="this.form.action='Send10_for.jsp'">포워드</button>
		<!-- 최초 : null / null -->		
	</form>
</div>
</body>
</html>

Send10_re.jsp ▼▼▼

<%@ page contentType="text/html; charset=UTF-8"%>
<%
	// 이전페이지로부터 데이터 수신
	// ①
	request.setCharacterEncoding("UTF-8");

	String userName = request.getParameter("userName");
	
	// 이 페이지에서 수행한 추가 작업
	// ②	
	request.setAttribute("message", "반갑습니다.");
	// --request 객체의 key(message)의 값 안에
	//   반갑습니다. 를 value 로 넣는 작업 수행
	
	// ※ 리다이렉트
	response.sendRedirect("Receive10.jsp");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Send10_re.jsp</title>
</head>
<body>

<div>
	<h1>데이터 송수신 실습10</h1>
	<hr>
</div>
<div>
	<h2>리다이렉트</h2>
</div>
<div>
	<p>이름 : <%=userName %></p>
</div>
</body>
</html>​

Receive10.jsp ▼▼▼

<%@ page contentType="text/html; charset=UTF-8"%>
<%
	// 이전 페이지로(Send10_re.jsp 또는 Send10_for.jsp)부터 데이터(userName, message) 수신
	
	request.setCharacterEncoding("UTF-8");

	String userName = request.getParameter("userName");
	
	
	// getAttribute("") 가 반환하는 것은 Object(객체임)
	// 따라서 String 으로 다운캐스팅 해서 변수에 담자
	String message = (String)request.getAttribute("message");
	
	
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Receive10.jsp</title>
</head>
<body>

<div>
	<h1>데이터 송수신 실습 10</h1>
</div>

<div>
	<p>이름 : <%=userName %></p>
	<p>내용 : <%=message %></p>
</div>
</body>
</html>

 

 

728x90

댓글