WEB/JSP

[JSP] JSP forward

다콩잉 2022. 9. 19. 17:01

forward: 웹 컨테이너 차원의 페이지 이동, 웹 브라우저는 다른 페이지로 이동했음을 알 수 없고, 

브라우저에 최초 호출한 URL이 표시(https://mangobab.tistory.com/16)

  1. 최초 요청 (클라이언트 -> request -> URL 1)
  2. forward (URL 1 -> forward -> URL 2)
  3. 응답 (URL 2 -> response -> 클라이언트)

 

 

 

page_control.jsp

<%--
  Created by IntelliJ IDEA.
  User: UserK
  Date: 2022-09-19
  Time: 오후 3:52
  To change this template use File | Settings | File Templates.
--%>
<%@ 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-0000-0000"/>
</jsp:forward>
</body>
</html>

 

page_control.jsp(이름 입력 페이지)

<%--
  Created by IntelliJ IDEA.
  User: UserK
  Date: 2022-09-19
  Time: 오후 3:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
  <h2>forward, sendRedirect 테스트</h2>
  <hr>
  <form method="post" action="forward_action2.jsp">
    forward action : <input type="text" name="username">
    <input type="submit" value="확인">
  </form>
</body>
</html>

 

 

forward_action2.jsp(forward 방식)

<%--
  Created by IntelliJ IDEA.
  User: UserK
  Date: 2022-09-19
  Time: 오후 3:52
  To change this template use File | Settings | File Templates.
--%>
<%@ 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-0000-0000"/>
</jsp:forward>
</body>
</html>

 

forward_action3.jsp(include 방식)

<%--
  Created by IntelliJ IDEA.
  User: UserK
  Date: 2022-09-19
  Time: 오후 3:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<jsp:include page="forward_action3.jsp">
    <jsp:param name="name" value='<%=request.getParameter("name")%>'/>
    <jsp:param name="tel" value="010-0000-0000"/>
</jsp:include>
</body>
</html>

 

 

forward_action3.jsp

<%--
  Created by IntelliJ IDEA.
  User: UserK
  Date: 2022-09-19
  Time: 오후 3:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>Title</title>
</head>
<body>
<center>
  <h2>forward action 및 sendRedirect() 결과</h2>
  <hr>
  지금 보이는 화면은 forward_action3.jsp 에서 출력한 결과 입니다.
  <hr>
  이름 : <%=request.getParameter("username")%>
  번호 : <%=request.getParameter("tel")%>
</center>
</body>
</html>

 

결과

 

 

 

 

page_control.jsp

<%--
  Created by IntelliJ IDEA.
  User: UserK
  Date: 2022-09-19
  Time: 오후 3:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
  <h2>forward, sendRedirect 테스트</h2>
  <hr>
  <form method="post" action="forward_action2.jsp">
    forward action : <input type="text" name="username">
    <input type="submit" value="확인">
  </form>
</body>
</html>

forward_action2.jsp

<%--
  Created by IntelliJ IDEA.
  User: UserK
  Date: 2022-09-19
  Time: 오후 3:52
  To change this template use File | Settings | File Templates.
--%>
<%@ 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-0000-0000"/>
</jsp:forward>
</body>
</html>

forward_action3.jsp

<%--
  Created by IntelliJ IDEA.
  User: UserK
  Date: 2022-09-19
  Time: 오후 3:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<jsp:forward page="forward_action4.jsp">
    <jsp:param name="tel" value="010-0000-0000"/>
</jsp:forward>
</body>
</html>

forward_action4.jsp

<%--
  Created by IntelliJ IDEA.
  User: UserK
  Date: 2022-09-19
  Time: 오후 3:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>Title</title>
</head>
<body>
<center>
  <h2>forward action 및 sendRedirect() 결과</h2>
  <hr>
  지금 보이는 화면은 forward_action4.jsp 에서 출력한 결과 입니다.
  <hr>
  이름 : <%=request.getParameter("username")%>
  번호 : <%=request.getParameter("tel")%>
</center>
</body>
</html>

결과

 

728x90