새소식

인기 검색어

풀스택개발자_대학교 정규과정/JSP_Example

jsp foward를 사용하여 parameter 값 전송

  • -

forward는 대상 자원으로 제어를 넘기는 역할을 합니다. 브라우저에서 /a.jsp로 요청했을 때 /a.jsp에서 forward()를 실행하여 /b.jsp로 제어를 넘길 수 있습니다. 제어를 넘겨받은 /b.jsp는 처리 결과를 최종적으로 브라우저에게 출력합니다. 브라우저 입장에서는 /a.jsp를 요청했지만 받은 결과는 /b.jsp의 결과입니다. 이때 HTTP 리다이렉트 방식과는 달리 하나의 HTTP 요청(Request) 범위 안에서 동작이 이루어집니다.

 

page_control.jsp(첫번째 jsp)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>page_control.jsp</title>
</head>
<body>
<h2>forward, sendredirect 테스트</h2>
<form action="forward_action2.jsp" method="post">
    forward action : <input type="text" name="username">
    <input type="submit" value="확인">
</form>

<form action="response_sendRedirect.jsp" method="post">
    respnse.sendRedirect : <input type="text" name="username">
    <input type="submit" value="확인">
</form>
</body>
</html>

forward_action2.jsp(두번째 jsp)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<jsp:forward page="forward_action3.jsp">
    <jsp:param name="tel" value="010-1234-5678"/>
</jsp:forward>
</body>
</html>

forward_action3.jsp(세번째 jsp)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<jsp:forward page="forward_action4.jsp"/>

</body>
</html>

 

 

forward_action4.jsp(네번째 jsp)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%request.setCharacterEncoding("UTF-8");%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>forward action 및 sendRedirect() 결과</h2>
<hr>
지금 보이는 화면은 forward_action4.jsp에서 출력한 결과입니다
<hr>
이름 : <%=request.getParameter("username")%><Br>
전화번호 : <%=request.getParameter("tel")%>
</body>
</html>

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.