JSP

Forward action tag를 이용한 데이터 출력 (페이지 4개)

충 민 2022. 9. 20. 02:31

Forward action tag란?

다른 페이지로 프로그램의 제어를 이동시킬 때 사용하는 액션 태그이다


JSP 페이지 내에서 forward 액션 태그를 만나게 되면, 그 전까지 출력 버퍼에 저장되어 있던 내용을 제거하고
forward 액션 태그가 지정하는 페이지로 이동한다
forward 액션 태그의 page 속성은 이동할 페이지명을 기술하고 상대경로, 절대경로로 지정할 수 있다

이 forward action 방법은 로그인 구현에서 흔히 쓰인다.  

이름을 입력받고 그 이름을 다음 액션태그jsp(forward_action2.jsp)로 넘겨주고

넘겨받은 이름과 저장해놓은 전화번호를

또 다음 액션태그jsp(forward_action3.jsp)에 넘겨준다.

넘겨준 액션태그에서 마지막페이지(page_control_end.jsp)로 넘겨준다.

다음 마지막 페이지에서 인코딩하여 setCharacterEncoding하여

request.getParameter()를 통하여 값을 가져온다. 그 후 마지막 페이지를 띄우지만 url창은 액션태그를 시작했던

jsp파일의 경로가 되게끔하였다.

page_control.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<br><br>
<form method=post action="forward_action2.jsp" align="center">
	forward action:<input type="text" name=username>
	<input type="submit" value="확인">
</form>

</body>
</html>

forward_action2.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>
<jsp:forward page="forward_action3.jsp">
<jsp:param name="tel" value="010-0000-0000"/>
</jsp:forward>


</body>
</html>

forward_action3.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>
 <jsp:forward page="page_control_end.jsp"/>

</body>
</html>

page_control_end.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>
<h2 align="center">forward action 및 sendRedirect() 결과</h2>
<hr>
	<%
	request.setCharacterEncoding("utf-8");
	String name = request.getParameter("username");
	String tel = request.getParameter("tel");
	%>

	<table align="center" border="1">
		<tr>
			<td>이름</td>
			<td><%=name%></td>
			<td>전화번호</td>
			<td><%=tel%></td>
		</tr>
	</table>

</body>
</html>