Search

νŽ˜μ΄μ§€ 이동 방식 - forward & redirect

νŽ˜μ΄μ§€ 이동 방식

β€’
forward
β€’
redirect
μ›Ή νŽ˜μ΄μ§€λ₯Ό μ΄λ™ν•˜λŠ” 방식은 2κ°€μ§€λ‘œ λ‚˜λˆ„μ–΄ λ³Ό 수 μžˆλ‹€.

forward 방식

forward : β€œμ „λ‹¬ν•˜λ‹€β€
β€’
μ„œλ²„ λ‚΄λΆ€μ—μ„œ μš”μ²­ 경둜 A κ°€ B 둜 μš”μ²­μ„ μ „λ‹¬ν•˜λ©° νŽ˜μ΄μ§€λ‘œ μ΄λ™ν•˜λŠ” 방식
β€’
νŠΉμ§•
β—¦
μ›λž˜μ˜ μš”μ²­ 정보(request) 및 응닡 정보(response)λ₯Ό κ·ΈλŒ€λ‘œ μ‚¬μš©
β—¦
ν΄λΌμ΄μ–ΈνŠΈλŠ” μ΄λ™ν•œ νŽ˜μ΄μ§€ B λ₯Ό 응닡 λ°›μ§€λ§Œ, μ£Όμ†Œ ν‘œμ‹œμ€„μ˜ μš”μ²­ URL 은 κ·ΈλŒ€λ‘œμž„
β—¦
쑰회(읽기) μš”μ²­μ„ ν•˜λŠ” κ²½μš°μ— 주둜 μ‚¬μš©

redirect 방식

redirect : β€œλ‹€μ‹œ μ§€μ‹œν•˜λ‹€β€
β€’
μ„œλ²„κ°€ μš”μ²­ 경둜 A 둜 μš”μ²­μ„ 받은 ν›„, ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ B 둜 λ‹€μ‹œ μš”μ²­ν•˜λΌκ³  μ§€μ‹œν•˜λ©° νŽ˜μ΄μ§€λ₯Ό μ΄λ™ν•˜λŠ” 방식
β€’
νŠΉμ§•
β—¦
ν΄λΌμ΄μ–ΈνŠΈκ°€ μ›λž˜μ˜ μš”μ²­κ³Ό 달리 μ™„μ „νžˆ λ‹€λ₯Έ μƒˆλ‘œμš΄ μš”μ²­μ„ ν•˜μ—¬, 기쑴의 μš”μ²­ μ •λ³΄λŠ” μ΄ν›„μ˜ μš”μ²­μ—μ„œλŠ” μ‚¬μš©ν•  수 μ—†μŒ
β—¦
μ΄λ™ν•œ ν™”λ©΄μ—μ„œ μ£Όμ†Œ ν‘œμ‹œμ€„μ˜ URL 이 A μ—μ„œ B 둜 변화됨
β—¦
등둝, μˆ˜μ •, μ‚­μ œ μš”μ²­ μ‹œμ— 주둜 μ‚¬μš©

forward λ°©μ‹μœΌλ‘œ νŽ˜μ΄μ§€ μ΄λ™ν•˜κΈ°

μ‹€μŠ΅ 파일

β€’
forward_A.jsp
β€’
forward.B.jsp

forward_A.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>νŽ˜μ΄μ§€ 이동 - Forward</title> </head> <body> <h1>νŽ˜μ΄μ§€ 이동 - Forward</h1> <h2>forward A ν™”λ©΄</h2> <jsp:forward page="forward_B.jsp" /> <p>-------------------------------</p> </body> </html>
HTML
볡사

forward_B.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>νŽ˜μ΄μ§€ 이동 - Forward</title> </head> <body> <h1>νŽ˜μ΄μ§€ 이동 - Forward</h1> <h2>forward B ν™”λ©΄</h2> <a href="../index.jsp">메인 νŽ˜μ΄μ§€</a> </body> </html>
HTML
볡사

κ²°κ³Ό ν™”λ©΄

redirect λ°©μ‹μœΌλ‘œ νŽ˜μ΄μ§€ μ΄λ™ν•˜κΈ°

μ‹€μŠ΅ 파일

β€’
redirect01_A.jsp
β€’
redirect01.B.jsp
β€’
RedirectServlet.java

redirect01_A.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>νŽ˜μ΄μ§€ 이동 - Redirect</title> </head> <body> <h1>νŽ˜μ΄μ§€ 이동 - Redirect</h1> <h2>redirect/redirect01_A.jsp</h2> <a href="redirect">νŽ˜μ΄μ§€ 이동 (redirect)</a> </body> </html>
HTML
볡사

redirect01_B.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>νŽ˜μ΄μ§€ 이동 - Redirect</title> </head> <body> <h1>νŽ˜μ΄μ§€ 이동 - Redirect</h1> <h2>redirect/redirect01_B.jsp</h2> <a href="../index.jsp">메인 νŽ˜μ΄μ§€</a> </body> </html>
HTML
볡사

RedirectServlet.java

package redirect; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/redirect/redirect") public class RedirectServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.sendRedirect("redirect01_B.jsp"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
HTML
볡사