Forward์™€ Redirect

์ฝœ๋ผ๋ง˜ยท2023๋…„ 6์›” 19์ผ
0

JSP

๋ชฉ๋ก ๋ณด๊ธฐ
7/17
post-thumbnail

๐Ÿ Forward

  • ์‚ฌ์šฉ์ž๊ฐ€ ๋ณด๋‚ธ ์š”์ฒญ์„ ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์—์„œ ์ฒ˜๋ฆฌํ•˜๋‹ค๊ฐ€ ์š”์ฒญ ์ •๋ณด๋ฅผ ๋‹ค๋ฅธ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๋„˜๊ฒจ ์ด์–ด ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ
  • ์‚ฌ์šฉ์ž๊ฐ€ ๋ณด๋‚ธ ์š”์ฒญ ๊ฐ์ฒด๋ฅผ ๊ทธ๋Œ€๋กœ ๋‹ค์Œ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ์ „๋‹ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์š”์ฒญ์— ์‹ค๋ ค์žˆ๋˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค๋„ ๊ทธ๋Œ€๋กœ ํ•จ๊ป˜ ์ „๋‹ฌ๋œ๋‹ค
  • ์ด ํฌ์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž๋ฐ” ์ฝ”๋“œ๊ฐ€ ๋งŽ์ด ์‚ฌ์šฉ๋œ ์ฒ˜๋ฆฌ(controller/service)๋Š” ์„œ๋ธ”๋ฆฟ์—์„œ ์ง„ํ–‰ํ•˜๊ณ , html ์ฝ”๋“œ๊ฐ€ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ์ฒ˜๋ฆฌ(view)๋Š” JSP์—์„œ ์ด์–ด์„œ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค

๐Ÿคบ Forward์˜ ์šฉ๋„

  • ์„œ๋ธ”๋ฆฟ์€ ์ž๋ฐ” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ์— ํŽธ๋ฆฌํ•˜๊ณ  JSP๋Š” html ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ์— ํŽธ๋ฆฌํ•˜๋‹ค
  • ํ•˜๋‚˜์˜ ์š”์ฒญ์— ๋Œ€ํ•ด์„œ 1์ฐจ๋กœ ์„œ๋ธ”๋ฆฟ์—์„œ ์ž๋ฐ”๋กœ ์ฒ˜๋ฆฌ๋ฅผ ๋ชจ๋‘ ๋งˆ์นœ ํ›„ ํฌ์›Œ๋“œ๋ฅผ ํ†ตํ•ด 2์ฐจ๋กœ html์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์‘๋‹ตํ•˜๋Š” ๋ฐฉ์‹์„ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค (์ด๊ฒƒ์„ view์™€ controller์˜ ๋ถ„๋ฆฌ๋ผ๊ณ  ํ•œ๋‹ค - MVC)
  • ์‹ค์ œ view ํŽ˜์ด์ง€์˜ ๊ฒฝ๋กœ๊ฐ€ ๋…ธ์ถœ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ forward๋ฅผ ๋ณด๋‚ธ ์„œ๋ธ”๋ฆฟ์˜ ์ฃผ์†Œ๊ฐ€ ๊ทธ๋Œ€๋กœ ์œ ์ง€๋œ๋‹ค(ํ™•์žฅ์ž๋ฅผ ๊ฐ์ถœ ์ˆ˜ ์žˆ๋‹ค)
  • forward๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†ก๋ฐ›์€ ํŽ˜์ด์ง€์—์„œ ์ƒˆ๋กœ๊ณ ์นจ์„ ๋ˆ„๋ฅด๋ฉด ๋‹ค์‹œ form์—์„œ submit์„ ๋ˆ„๋ฅด๋Š” ๊ฒƒ์œผ๋กœ ์—ฌ๊ธฐ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž๊ฐ€ ์š”์ฒญ์— ํ•จ๊ป˜ ์‹ค์–ด๋ณด๋ƒˆ๋˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋ฐ›๊ฒŒ๋œ๋‹ค

๐Ÿ’ฌ ์ฝ”๋“œ >>
โœ” index.jsp

<form id="form1" action="/chap03/forwardtest" method="POST"></form>
		<input type="text" name="type" value="drink" form="form1"/>
		<input type="text" name="name" value="Americano" form="form1"/>
		<input type="text" name="price" value="7000" form="form1"/>
		<input type="text" name="qty" value="5" form="form1"/>
		<input type="submit" value="๋ณด๋‚ด๊ธฐ" form="form1"/>

โœ” java(servlet)ํŒŒ์ผ

@WebServlet("/forwardtest")
public class ForwardServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		// ์ž๋ฐ”๋ฅผ ์‚ฌ์šฉํ•ด ์ž๋ฐ”๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ์€ ๋‚ด์šฉ๋“ค์„ ์ฒ˜๋ฆฌํ•œ๋‹ค ... 
		String type = req.getParameter("type");
		String name = req.getParameter("name");
		String price = req.getParameter("price");
		String qty = req.getParameter("qty");
		int total_price = 0;
		
		// String์œผ๋กœ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•  ๋•Œ null์ด ๋“ค์–ด์žˆ์„ ๊ฒƒ์„ ๋Œ€๋น„ํ•ด์•ผ ํ•œ๋‹ค
		if(price!=null && qty !=null) { // ๋‘˜๋‹ค null์ด ์•„๋‹๋•Œ ๋งŒ parseint
			System.out.printf("%s(%s) / %d\n",name, type, 
					total_price = (Integer.parseInt(price)*Integer.parseInt(qty)));
		}else {
			System.out.printf("%s(%s) / undefined\n", name, type);
		}
		
		//3.
		// ์ฒ˜๋ฆฌ ํ›„ ์–ป์–ด๋‚ธ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์Œ ์„œ๋ธ”๋ฆฟ๊นŒ์ง€ ์ „๋‹ฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š”
		// request๊ฐ์ฒด์— ๋ฐ์ดํ„ฐ๋ฅผ ์‹ค์–ด ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค (attribute)
		req.setAttribute("full_name", String.format("%s(%s)", name, type));
		req.setAttribute("total_price", total_price);
		
		//2.
		// ์š”์ฒญ์„ ๋‹ค๋ฅธ๊ณณ์œผ๋กœ ๋„˜๊ธฐ๊ธฐ ์œ„ํ•œ ๊ธฐ๋Šฅ๋“ค์ด ๋“ค์–ด์žˆ๋Š” ๊ฐ์ฒด
		// forward : ์š”์ฒญ ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฅธ๊ณณ์œผ๋กœ ๋„˜๊ฒจ ์ฒ˜๋ฆฌํ•œ ํ›„ ๊ทธ๊ณณ์—์„œ ์‘๋‹ตํ•œ๋‹ค
		// include : ์š”์ฒญ ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฅธ๊ณณ์œผ๋กœ ๋„˜๊ฒจ ์ฒ˜๋ฆฌํ•œ ํ›„ ๋‹ค์‹œ ๋Œ์•„์˜จ๋‹ค
		RequestDispatcher dispatcher = req.getRequestDispatcher("/forward/view.jsp");

		dispatcher.forward(req, resp);
	}
}

โœ” view.jsp (๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ฒ˜๋ฆฌ๊ฐ€ ๋ชจ๋‘ ๋๋‚œ ํ›„ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ง€๋Š” ํŽ˜์ด์ง€/view)

<h3># ์ด์ „ ์„œ๋ธ”๋ฆฟ์œผ๋กœ๋ถ€ํ„ฐ ๋„˜๊ฒจ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋“ค</h3>
ํ’€ ๋„ค์ž„ : <%=request.getAttribute("full_name") %><br>
์ด ๊ฐ€๊ฒฉ : <%=request.getAttribute("total_price") %><br>

๐Ÿ“บ ํ™”๋ฉด ์ถœ๋ ฅ >>


๐Ÿ Redirect

  • ์š”์ฒญ์„ ๋ณด๋‚ธ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋‹ค๋ฅธ ์ฃผ์†Œ(์‚ฌ์šฉ์ž์˜ ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ํ•„์š”ํ•œ ๊ฒฝ๋กœ)๋กœ ๋‹ค์‹œ ์š”์ฒญํ•˜๋ผ๋Š” ์‘๋‹ต์„ ๋ณด๋‚ด๋Š” ๊ฒƒ
  • ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ์‘๋‹ต์„ ๋ฐ›์€ ํด๋ผ์ด์–ธํŠธ(์›น ๋ธŒ๋ผ์šฐ์ €)์—์„œ๋Š” ์ƒˆ๋กœ์šด ์š”์ฒญ์„ ๋งŒ๋“ค์–ด ๋ณด๋‚ด๊ธฐ ๋•Œ๋ฌธ์— ์ด์ „ ์š”์ฒญ์„ ๋ณด๋ƒˆ๋˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” (๊ธฐ๋ณธ์ ์œผ๋กœ๋Š”) ๋‚จ์•„์žˆ์„ ์ˆ˜ ์—†๋‹ค (์š”์ฒญ ๊ฐ์ฒด๋ฅผ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ƒˆ ์š”์ฒญ์„ ๋ณด๋‚ด๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์œ ์ง€๋˜์ง€ ์•Š๋Š”๋‹ค)

๐Ÿคบ Redirect์˜ ์šฉ๋„

  • ์‚ฌ์šฉ์ž๊ฐ€ ์ •์ƒ์ ์ด์ง€ ์•Š์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ ‘์†ํ•œ ๊ฒฝ์šฐ๋ฅผ ๋ง‰๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ
  • ์‚ฌ์šฉ์ž๊ฐ€ ์š”์ฒญ์— ํ•จ๊ป˜ ์‹ค์–ด๋ณด๋ƒˆ๋˜ ๋ฐ์ดํ„ฐ๋“ค์„ ์ƒˆ๋กœ๊ณ ์นจ์œผ๋กœ ์žฌ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๋„๋ก ๋ง‰๋Š” ์šฉ๋„
    ex> ์‚ฌ์šฉ์ž๊ฐ€ ๊ธ€์„ ์ž‘์„ฑํ•œ ํ›„ ์ƒˆ๋กœ๊ณ ์นจ์„ ๋ˆŒ๋ €์„ ๋•Œ ๊ฐ™์€ ๊ธ€์ด ๊ณ„์† ๋“ฑ๋ก๋˜๋Š” ๊ฒƒ์„ ๋ง‰์•„์•ผ ํ•œ๋‹ค

๐Ÿ’ฌ ์ฝ”๋“œ >>
โœ” index.jsp

<form id="form2" action="/chap03/redirect_test" method="POST">
	</form>
		<input type="text" name="type" value="drink" form="form2"/>
		<input type="text" name="name" value="Americano" form="form2"/>
		<input type="text" name="price" value="7000" form="form2"/>
		<input type="text" name="qty" value="5" form="form2"/>
		<input type="submit" value="๋ณด๋‚ด๊ธฐ" form="form2"/>

โœ” java(servlet)ํŒŒ์ผ

@WebServlet("/redirect_test")
public class RedirectServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
resp.sendRedirect("/chap03/forward/view.jsp");
	
	}
}
profile
์ฝœ๋ผ๋ง˜ ์ฝ”๋”ฉ์ผ๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€