
2025๋ 4์ 2์ผ
Redirect๋ ์๋ฒ๊ฐ ํด๋ผ์ด์ธํธ์๊ฒ ์๋ก์ด ์์ฒญ ๊ฒฝ๋ก(URL)๋ฅผ ์๋ต์ผ๋ก ์ ๋ฌํ๋ ๋ฐฉ์์ด๋ค.
ํด๋ผ์ด์ธํธ๋ ํด๋น ๊ฒฝ๋ก๋ก ๋ค์ ์์ฒญํ๊ฒ ๋๋ฉฐ, ์์ ํ ์๋ก์ด ์์ฒญ-์๋ต ์ฃผ๊ธฐ๊ฐ ์์๋๋ค.
request ๊ฐ์ฒด์ response ๊ฐ์ฒด๋ ์ ์ง๋์ง ์์setAttribute()๋ก ์ ์ฅํ ๊ฐ์ ์ ๋ฌ๋์ง ์์response.sendRedirect("url")๋ฅผ ํธ์ถํ๋ฉด
์๋ฒ๋ HTTP 302 ์๋ต + Location ํค๋๋ฅผ ํด๋ผ์ด์ธํธ์ ๋ณด๋ด๊ณ ,
ํด๋ผ์ด์ธํธ๋ ํด๋น URL๋ก ์๋ก์ด GET ์์ฒญ์ ๋ค์ ๋ณด๋ธ๋ค.
01Form.html
โ (submit)
02Page.jsp
โ (redirect)
03Page.jsp
โ (redirect)
04Result.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Form</title>
</head>
<body>
<form action="02Page.jsp">
<input type="text" name="username" />
<input type="text" name="password" />
<button>์ ์ก</button>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.net.*" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("----------02PAGE---------------");
System.out.println("username :" + username);
System.out.println("password :" + password);
System.out.println("-------------------------------");
request.setAttribute("02Page", "02PageValue");
// ํ๊ธ ํ๋ผ๋ฏธํฐ ์ธ์ฝ๋ฉ ํ์!
response.sendRedirect("./03Page.jsp?hobby=" + URLEncoder.encode("๋ฑ์ฐ", "UTF-8"));
%>
โ Redirect ์ URL์ ํ๊ธ์ด ํฌํจ๋๋ฉด ๋ฐ๋์ URLEncoder.encode(๊ฐ, "UTF-8")๋ก ์ธ์ฝ๋ฉํด์ผ ๊นจ์ง์ง ์์
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String Page02Value = (String) request.getAttribute("02Page"); // null
String hobby = request.getParameter("hobby");
System.out.println("----------03PAGE---------------");
System.out.println("username :" + username);
System.out.println("password :" + password);
System.out.println("Page02Value : " + Page02Value); // null
System.out.println("hobby : " + hobby);
System.out.println("-------------------------------");
response.sendRedirect("./04Result.jsp");
%>
โ ๏ธ username, password, Page02Value๋ ๋ชจ๋ null๋ก ์ถ๋ ฅ๋๋ค.
๊ทธ ์ด์ ๋
response.sendRedirect()๋ก ์ธํด request ๊ฐ์ฒด๊ฐ ์๋ก ์์ฑ๋์๊ธฐ ๋๋ฌธ!
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>RESULT</title>
</head>
<body>
<h1>04 RESULT</h1>
</body>
</html>
----------02PAGE---------------
username : testuser
password : 1234
-------------------------------
----------03PAGE---------------
username : null
password : null
Page02Value : null
hobby : ๋ฑ์ฐ
-------------------------------
04 RESULT
| ํญ๋ชฉ | getParameter() | getAttribute() |
|---|---|---|
| ์ฌ์ฉ ๋ชฉ์ | form ์ ๋ ฅ๊ฐ ์์ | JSP ๊ฐ ์๋ฒ ๋ด๋ถ ๋ฐ์ดํฐ ์ ๋ฌ |
| ๋ฐ์ดํฐ ์ถ์ฒ | ํด๋ผ์ด์ธํธ | ์๋ฒ ์ธก์์ ์ ์ฅ |
| ์ ์ง ์ฌ๋ถ | URL ํ๋ผ๋ฏธํฐ๋ก ๋๊ธฐ๋ฉด ์ ์ง | Forward ์์๋ง ์ ์ง, Redirect ์ ์ฌ๋ผ์ง |
| ์ฌ์ฉ ์ | request.getParameter("username") | request.getAttribute("02Page") |
| ํญ๋ชฉ | Forward | Redirect |
|---|---|---|
| URL ๋ณ๊ฒฝ | โ ๊ทธ๋๋ก ์ ์ง | โ ๋ณ๊ฒฝ๋จ |
| request ์ ์ง | โ ์ ์ง๋จ | โ ์๋ก ์์ฑ๋จ |
| ์๋ | ๋น ๋ฆ (๋ด๋ถ ์ด๋) | ๋๋ฆผ (์ธ๋ถ ์ด๋) |
| ์ฉ๋ | ๋ด๋ถ ํ๋ฆ ์ฐ๊ฒฐ | ํ์ด์ง ์ด๋, ์ ์์ฒญ ์ฒ๋ฆฌ |
| setAttribute ์ ๋ฌ | โ ๊ฐ๋ฅ | โ ๋ถ๊ฐ |
| ํ๋ผ๋ฏธํฐ ์ ๋ฌ | ๋ด๋ถ ๊ณต์ | URL ์ฟผ๋ฆฌ์คํธ๋ง ์ฌ์ฉ |
| ์ํฉ | Forward ์ฌ์ฉ | Redirect ์ฌ์ฉ |
|---|---|---|
| ๋ก๊ทธ์ธ ์ฒ๋ฆฌ ํ | O (๋ด๋ถ ์ฒ๋ฆฌ ํ view ์ด๋) | O (๋ก๊ทธ์ธ ํ ๋ฉ์ธ ํ์ด์ง ์ด๋) |
| ๊ฒ์๊ธ ์์ฑ/์์ ํ | โ (์ค๋ณต ์ ์ก ๊ฐ๋ฅ์ฑ) | โ (์๋ฃ ํ ๋ชฉ๋ก ์ด๋) |
| ๋จ์ ์กฐํ/์ถ๋ ฅ ํ์ด์ง | โ (์๋ฒ ๋ด๋ถ ์ฐ๊ฒฐ) | โ (๋ถํ์) |
| ์ ํจ์ฑ ๊ฒ์ฌ ์คํจ | โ (์ ๋ ฅ๊ฐ ์ ์ง ๊ฐ๋ฅ) | โ (๊ฐ ์ฌ๋ผ์ง) |
| ๋ช ์์ URL ์ด๋ | โ | โ |
| request ๋ฐ์ดํฐ ์ ๋ฌ ํ์ | โ | โ |
response.sendRedirect()๋ ์๋ฒ๊ฐ ํด๋ผ์ด์ธํธ์๊ฒ ์๋ก์ด URL์ ์๋ตํ๋ ๋ฐฉ์์ด๋ค.request.setAttribute()๋ฅผ ํตํ ๊ฐ ์ ๋ฌ์ ๋ถ๊ฐ๋ฅํ๋ค.URLEncoder๋ก ์ธ์ฝ๋ฉํด์ผ ๊นจ์ง์ง ์๋๋ค.์ด๋ฒ ์ค์ต์ ํตํด Redirect ๋ฐฉ์์ ํ๋ฆ์ ๋ช ํํ ์ดํดํ ์ ์์๊ณ ,
request ๊ฐ์ฒด๊ฐ ์๋ก ์์ฑ๋๋ค๋ ์ , ๊ทธ๋ฆฌ๊ณ setAttribute()๋ก ๊ฐ์ ์ ๋ฌํ ์ ์๋ค๋ ์ ์ ์ค์ต์ ํตํด ๋ช
ํํ ์ฒด๊ฐํ ์ ์์๋ค.
์ฒ์์๋ Forward์ฒ๋ผ ๋์ํ ์ค ์๊ณ ๊ฐ์ ๋๊ฒผ๋ค๊ฐ null์ด ๋ ์ ๋นํฉํ๋ ๊ฒ๋ ๊ธฐ์ต๋๋ค ๐
์ค๋ฌด์์ Redirect๋ ๋ก๊ทธ์ธ ์ฒ๋ฆฌ, ๊ฒ์๊ธ ๋ฑ๋ก ํ ์ด๋, ์ค๋ณต ๋ฐฉ์ง ๋ฑ์ ์ํฉ์์ ์์ฃผ ์ฌ์ฉ๋ ์ ์์ผ๋ ๊ผญ ์ตํ๋ฌ์ผ๊ฒ ๋ค.
setAttribute๋ ์ ๋ฌ๋์ง ์๋๋ค.