WEB Archive
[HTML] 포워딩과 리다이렉트 실습 - RequestDispatcher
universedevelope
2024. 8. 22. 08:23
Send11.html ▼▼▼
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Send11.html</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<div>
<h1>데이터 송수신 실습 11</h1>
<hr>
</div>
<!-- ■■■ 포워딩 / 리다이렉트 관련 중요한 실습 ■■■ -->
<!-- ① 사용자 최초 요청 페이지 -->
<!-- 사칙연산 수행을 위한 정수 입력페이지 구성 -->
<!-- 연산자를 함께 입력받을 수 있도록 처리 -->
<!-- 정수1 / 정수2 / 연산자 -->
<!-- http://localhost:8090/WebApp07/Send11.jsp -->
<!-- ② 연산 전용 페이지 -->
<!-- 스크립트릿 코드만 존재 + jsp:forward 액션 태그 -->
<!-- → 추후 이 코드를 java 로 분리하여 → Servlet 으로 구성할 예정 -->
<!-- http://localhost:8090/WebApp07/Forward11.jsp -->
<!-- ③ 최종 결과 출력 페이지 -->
<!-- 연산 전용 페이지에서 처리한 결과를 넘겨받아 클라이언트와 대면할 페이지로 구성 -->
<!-- → 추후 이 페이지는 jsp view 페이지의 역할을 수행할 예정 -->
<!-- http://localhost:8090/WebApp07/Receive11.jsp -->
<div>
<!-- jsp:forward 액션 태그 사용 -->
<!-- <form action="Forward11.jsp" method="post"> -->
<!-- jsp:forward 액션 태그 없이 포워딩 처리 -->
<form action="Forward11_1.jsp" method="post">
정수1 <input type="text" name="num1" class="txt">
<select name="calResult">
<option selected="selected">연산 선택</option>
<option value="1">더하기</option>
<option value="2">빼기</option>
<option value="3">곱하기</option>
<option value="4">나누기</option>
</select>
정수2 <input type="text" name="num2" class="txt">
<button type="submit" class="btn control">확인</button>
</form>
</div>
</body>
</html>
Forward11.jsp ▼▼▼
<%@ page contentType="text/html; charset=UTF-8"%>
<%
// Forward11.jsp
// 이전 페이지(Send11.html)로부터 데이터(num1, calResult, num2) 수신
String num1Str = request.getParameter("num1");
String num2Str = request.getParameter("num2");
String calResult = request.getParameter("calResult");
int num1=0;
int num2=0;
String result ="";
try
{
num1 = Integer.parseInt(num1Str);
num2 = Integer.parseInt(num2Str);
if(calResult.equals("1")) // 더하기
result = String.format("%d + %d = %d", num1, num2, (num1+num2));
else if(calResult.equals("2")) // 빼기
result = String.format("%d - %d = %d", num1, num2, (num1-num2));
else if(calResult.equals("3")) // 곱하기
result = String.format("%d * %d = %d", num1, num2, (num1*num2));
else if(calResult.equals("4")) // 나누기
result = String.format("%d / %d = %.0f", num1, num2, (num1/(double)num2));
}
catch(Exception e)
{
System.out.println(e.toString());
}
request.setAttribute("resultStr", result);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Forward11.jsp</title>
</head>
<body>
<jsp:forward page="Receive11.jsp"></jsp:forward>
</body>
</html>
Receive11.jsp ▼▼▼
<%@ page contentType="text/html; charset=UTF-8"%>
<%
// 이전 페이지 (Forward11.jsp)로부터 데이터 수신
String result = (String)request.getAttribute("resultStr");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Receive11.jsp</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<div>
<h1> 데이터 송수신 실습 11</h1>
<hr>
</div>
<div>
<h2>연산 결과 : <%=result %></h2>
</div>
</body>
</html>
728x90