본문 바로가기
WEB Archive

[HTML] (Redirect) 리다이렉트 실습

by universedevelope 2024. 8. 22.

Send12.html 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Send12.html</title><link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>

<div>
	<h1>데이터 송수신 실습 12</h1>
	<hr>
</div>
<!-- ■■■ 포워딩 / 리다이렉트 관련 중요한 실습 ■■■ -->
<!-- ① 사용자 최초 요청 페이지 -->
<!--    사칙연산 수행을 위한 정수 입력페이지 구성 -->
<!--    연산자를 함께 입력받을 수 있도록 처리 -->
<!--    정수1 / 정수2 / 연산자 -->
<!--    http://localhost:8090/WebApp07/Send12.jsp  -->

<!-- ② 연산 전용 페이지 -->
<!--    reponse.sendRedirect()를 포함한 스크립트릿 코드만 존재
<!--    → 추후 이 코드를 java 로 분리하여 → Servlet 으로 구성할 예정 -->
<!--    http://localhost:8090/WebApp07/Forward11.jsp  -->

<!-- ③ 최종 결과 출력 페이지 -->
<!--    연산 전용 페이지에서 처리한 결과를 넘겨받아 클라이언트와 대면할 페이지로 구성 -->
<!--    → 추후 이 페이지는 jsp view 페이지의 역할을 수행할 예정 -->
<!--    http://localhost:8090/WebApp07/Receive11.jsp  -->


<div>
	
	
	<!-- 리다이렉트 처리 페이지로 요청 -->
	<form action="Redirect12.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>

Redirect12.jsp  

<%@ page contentType="text/html; charset=UTF-8"%>
<%
	// Redirect12.jsp
	
	// 이전페이지(Send12.html)로부터 데이터(num1, cal, num2, result)
	int num1 = Integer.parseInt(request.getParameter("num1"));
	int num2 = Integer.parseInt(request.getParameter("num2"));
	String op = request.getParameter("calResult");
	
	String str="";
	if(op.equals("1"))
		str += String.format("%d", (num1+num2));
	else if(op.equals("2"))
		str += String.format("%d", (num1-num2));
	else if(op.equals("3"))
		str += String.format("%d", (num1*num2));
	else if(op.equals("4"))
		str += String.format("%.1f", (num1/(double)num2));
	
	// check~!!
	// 결과 데이터 재전송 → sendRedirect()메소드 사용
	// ※ response 객체의 주요 메소드중 하나인
	// 『sendRedirect(String location)』
	//      : 지정된 URL(location)으로 요청을재전송한다.
	/* response.sendRedirect("Receive12.jsp") */;
	// → 서버의 응답이 해당 링크로 보내주는것
	
	
	// ↓get방식으로 주는거
	response.sendRedirect("Receive12.jsp?num1="+num1+"&num2="+num2+"&op="+op+"&str="+str);
	 
	
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

</body>
</html>

Receive12.jsp 

<%@ page contentType="text/html; charset=UTF-8"%>
<%
	// 이전 페이지로부터(Redirect12.jsp) → num1="+num1+"&num2="+num2+"&op="+op+"&str="+str
	// num1 num2 op str 4개
	String num1 = request.getParameter("num1");
	String num2 = request.getParameter("num2");
	String op = request.getParameter("op");
	String str = request.getParameter("str");
	
	if(op.equals("1"))
		op ="+";
	else if(op.equals("2"))
		op ="-";
	else if(op.equals("3"))
		op ="*";
	else if(op.equals("4"))
		op ="/";
	
	String strResult = String.format("%s %s %s = %s", num1, op, num2, str);
	
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Receive12.jsp</title>
</head>
<body>

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

<div>
	연산결과 : <%=strResult %>
</div>
</body>
</html>
728x90

댓글