νμ΄μ§ μ΄λ λ°©μ
β’
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
볡μ¬