
2025๋ 4์ 2์ผ
Forward๋ ์๋ฒ ๋ด๋ถ์์ ์์ฒญ์ ๋ค๋ฅธ JSP/Servlet์ผ๋ก ์ ๋ฌํ๋ ๋ฐฉ์์ด๋ค.
์์ฒญ์ ํด๋ผ์ด์ธํธ์๊ฒ ์ฌ์ ์ก๋์ง ์๊ณ , ์๋ฒ ๋ด๋ถ์์ ๋ค์ ๊ฒฝ๋ก๋ก ์ด๋ํ๋ค.
RequestDispatcher.forward(request, response) ๋ก ์ฌ์ฉ01Form.html
โ (submit)
02Page.jsp
โ (forward)
03Page.jsp
โ (forward)
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"%>
<%
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");
request.getRequestDispatcher("./03Page.jsp").forward(request, response);
%>
<%@ 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");
System.out.println("----------03PAGE---------------");
System.out.println("username :" + username);
System.out.println("password :" + password);
System.out.println("Page02Value : " + Page02Value);
System.out.println("-------------------------------");
request.setAttribute("03Page", "03Page's Value");
request.getRequestDispatcher("./04Result.jsp").forward(request, response);
%>
<%@ 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");
String Page03Value = (String) request.getAttribute("03Page");
System.out.println("----------RESULT---------------");
System.out.println("username :" + username);
System.out.println("password :" + password);
System.out.println("Page02Value : " + Page02Value);
System.out.println("Page03Value : " + Page03Value);
System.out.println("-------------------------------");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>RESULT</title>
</head>
<body>
<h1>RESULT PAGE</h1>
USERNAME: <%= username %><br/>
PASSWORD: <%= password %><br/>
02PAGE VALUE: <%= Page02Value %><br/>
03PAGE VALUE: <%= Page03Value %><br/>
</body>
</html>
----------02PAGE---------------
username : testuser
password : 1234
-------------------------------
----------03PAGE---------------
username : testuser
password : 1234
Page02Value : 02PageValue
-------------------------------
----------RESULT---------------
username : testuser
password : 1234
Page02Value : 02PageValue
Page03Value : 03Page's Value
-------------------------------
RESULT PAGE
USERNAME: testuser
PASSWORD: 1234
02PAGE VALUE: 02PageValue
03PAGE VALUE: 03Page's Value
| ํญ๋ชฉ | getParameter() | getAttribute() |
|---|---|---|
| ์ฌ์ฉ ์์น | form ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ ๋ | JSP ๊ฐ ๋ฐ์ดํฐ ๊ณต์ ์ |
| ๋ฐ์ดํฐ ์ถ์ฒ | ํด๋ผ์ด์ธํธ ์์ฒญ (HTML ํผ ๋ฑ) | ์๋ฒ ๋ด์์ setAttribute๋ก ์ ์ฅํ ๊ฐ |
| ๋ฐ์ดํฐ ์ ์ง | ํฌ์๋ ์ ์ ์ง๋จ | ํฌ์๋ ์์๋ง ์ ์ง๋จ |
| ์ฌ์ฉ ์ | request.getParameter("username") | request.getAttribute("02Page") |
forward()๋ ์๋ฒ ๋ด๋ถ์์ ํ์ด์ง๋ฅผ ๋๊ฒจ์ฃผ๋ ๋ฐฉ์์ผ๋ก, request ๊ฐ์ฒด๊ฐ ์ ์ง๋๋ค.setAttribute()๋ฅผ ํตํด JSP ๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ์ ์๊ณ , ์ด ๊ฐ์ getAttribute()๋ก ๋ค์ JSP์์ ๋ฐ์ ์ถ๋ ฅํ ์ ์๋ค.getParameter()๋ HTML form์์ ๋์ด์จ ๊ฐ์ด๊ณ , getAttribute()๋ ์๋ฒ ๋ด๋ถ์์ ๊ณต์ ํ๋ ๋ฐ์ดํฐ์ด๋ค.์ด๋ฒ ์ค์ต์ ํตํด JSP์์ Forward ํ๋ฆ์ ๊ฐ๋ ๊ณผ ์ค์ ๋์ ๋ฐฉ์์ ์ฒด๊ณ์ ์ผ๋ก ์ตํ ์ ์์๋ค.
ํนํ request ๊ฐ์ฒด๊ฐ ์ด๋ป๊ฒ ์ ์ง๋๊ณ , ์ค๊ฐ์ค๊ฐ setAttribute๋ก ๊ฐ์ ์ ๋ฌํ๋ ๊ณผ์ ์ด ์ค์ํ ํฌ์ธํธ์๋ค.
ํฅํ์๋ ์ด ๊ตฌ์กฐ๋ฅผ ๋ฐํ์ผ๋ก MVC ํจํด์์ Controller โ View๋ก์ Forward ํ๋ฆ์ ๊ตฌํํด๋ณด๊ณ ์ถ๋ค.
forward()๋ ํ๋์ ์์ฒญ ํ๋ฆ์ผ๋ก ์ฌ๋ฌ JSP๋ฅผ ์ฐ๊ฒฐํ๋ค.setAttribute()๋ก ์ด๋ฃจ์ด์ง๋ค.