ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • (JSP) 포워딩과 리다이렉트
    서버 프로그래밍 2021. 8. 26. 17:33

    JSP에서 다른 페이지로 전환하는 방식은 크게 두가지로 포워딩(Forwarding)과 리다이렉트(Redirect)방식이 있다.

    간단한 코드 예제와 함께 이 두 가지 방식의 차이점에 대해서 알아보자.

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>포워딩 vs 리다이렉팅</title>
    </head>
    <body>
     
    <%-- 포워딩 방식 --%>
    <form action="forward.jsp" method="post">
        <input type="text" name="username">
        <input type="submit" name="페이지 이동">
    </form>
     
    <%-- 리다이렉팅 방식 --%>
    <form action="response_sendRedirect.jsp" method="post">
        <input type="text" name="username">
        <input type="submit" name="페이지 이동">
        
    </form>
    </body>
    </html>
    cs

    < 포워딩과 리다이렉팅 두 가지 방식으로 페이지 이동을 해보자 >

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>결과 페이지</title>
    </head>
    <body>
     
    <h1><%=request.getParameter("username")%></h1>
    <h1><%=request.getParameter("email")%></h1>
     
    </body>
    </html>
    cs

    < 두 방식으로 넘어올 페이지 >


    포워딩(Forwarding)


    웹 컨테이너(Web Container) 차원에서 페이지 이동만 하는 방식으로 URL이 변경되지 않는다.

    그렇기 때문에 클라이언트측에서 페이지가 변경되었는지 알 수 없다.

    현재 페이지와 포워딩에 의해서 호출된 페이지는 request(요청), response(응답) 객체를 공유하기 때문에

    사용자가 최초로 요청한 요청정보는 다음 URL 에서도 유효하게 된다.

     

    * 클라이언트와 통신 없이 서버에서만 처리되기 때문에 리다이렉트 방식보다 나은 성능을 보여준다.

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%
       request.setCharacterEncoding("UTF-8");
    %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>포워딩 방식</title>
    </head>
    <body>
     
    <jsp:forward page="result.jsp">
       <jsp:param value="h-coding.tistory.com" name="email"/>
    </jsp:forward>
     
    </body>
    </html>
    cs

     

    포워딩 방식으로 result.jsp 로 이동할 때, 추가 인자로 email 정보를 넣어주었다.

     


    리다이렉트(Redirect)


    response 객체를 주체로 한 리다이렉트 방식은 포워딩 방식과는 다르게 페이지 전환/이동 시 request, response 객체를

    새로 생성한다. 또한 sendRedirect 명령에는 브라우저가 웹 컨테이너의 응답을 받은 후 다시 요청을 보낼 새로운 URL을

    포함하기 때문에 웹 브라우저의 URL이 명시된 주소로 바뀌게 된다.
    요청정보가 초기화 되기 때문에 이전에 요청한 정보를 표시할 수 없다. 밑의 결과를 보자.

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>리다이렉트 방식</title>
    </head>
    <body>
     
    <% response.sendRedirect("result.jsp"); %>
     
    </body>
    </html>
    cs

     


    결과 화면


     

    첫 번째는 포워딩, 두 번째는 리다이렉트 방식이다.

    포워딩 방식에서는 URL이 forward.jsp로 바뀌지 않는 것을 볼 수 있고, 

    리다이렉트 방식에서는 request 객체가 초기화 되어서 null이 출력되는걸 볼 수 있다.

Designed by Tistory.