[JSP] μ‘μ νκ·Έ - forward / include / param
μ‘μ νκ·Έ (Action Tag)
-
JSP νμ΄μ§ λ΄μμ μ΄λ€ λμμ μννλλ‘ μ§μνλ νκ·Έ
-
XML κ·μΉμ λ°λΌμΌ ν¨ (
<jsp:action />
) / XMLμλ λ°μ΄ν°λ₯Ό μ μ‘, μ λ¬ λ°λ κΈ°λ₯μ΄ μλ€. -
μ‘μ νκ·Έμ μ’ λ₯
-
jsp:forward
: λ€λ₯Έ νμ΄μ§λ‘ μ΄λ, νμ΄μ§μ νλ¦μ μ μ΄ -
jsp:include
: νμ¬ νμ΄μ§μ λ€λ₯Έ νμ΄μ§λ₯Ό μ½μ , νμ΄μ§ λͺ¨λν -
jsp:useBean
: μλ° μΈμ€ν΄μ€(Java Bean) μμ±, νκ²½ μ μ -
jsp:setProperty
: μλ° λΉμ μμ±κ° μ€μ -
jsp:getProperty
: μλ° λΉμ μμ±κ°μ μ»μ -
jsp:plugin
: μΉ λΈλΌμ°μ μ μ’ λ₯μ λ°λΌ κ·Έμ λ§λ μλ° νλ¬κ·ΈμΈμ μ¬μ©νκΈ° μν HTML νκ·Έ (OBJECT or EMBED)λ₯Ό μμ± -
jsp:param
: forward, include, pluginκ³Ό κ°μ΄ μ¬μ©ν΄ μΈμ μΆκ°
-
μ‘μ νκ·Έ - include
-
νμ¬ νμ΄μ§μ λ€λ₯Έ νμ΄μ§λ₯Ό μ½μ (HTML, JSP, μλΈλ¦Ώ νμ΄μ§ λ±)
-
νμ΄μ§λ₯Ό λͺ¨λν ν΄ μ½λκ° μ€λ³΅λλ κ²μ λ§μ μ μμ
-
page μμ± κ° : νμ¬ JSP νμ΄μ§ λ΄μ ν¬ν¨ν λ΄μ©μ κ°μ§ μΈλΆ νμΌλͺ , νμ¬ νμ΄μ§μ κ°μ λλ ν°λ¦¬μ μμ§ μλ€λ©΄ μ 체 URL λλ μλ κ²½λ‘λ₯Ό μ§μ ν΄μΌ ν¨
-
flush μμ± κ° : μ€μ ν μΈλΆ νμΌλ‘ μ μ΄ μ΄λ μ νμ¬ JSP νμ΄μ§κ° μ§κΈκΉμ§ μΆλ ₯ λ²νΌμ μ μ₯ν κ²°κ³Όλ₯Ό μ²λ¦¬, κΈ°λ³Έ κ°μ false
- μ§μμ includeνκ·Έλ λ κ°μ μ½λκ° λΆμ λ€μμ μ 체 μ€ννλ κ²°κ³Ό / μ‘μ νκ·Έλ νμ¬ νμ΄μ§ μ€ν μ€ λ§λλ©΄ ν΄λΉ νμ΄μ§λ‘ κ°μ μ€ν ν λμμ€λ, μ¦ κ°κ° μ€ν ν λ€μμ λΆμ΄λ κ²°κ³Ό
-
ex)
-
Include.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>μ‘μ νκ·Έ - include</title> </head> <body> <h1>include 1</h1> <jsp:include page="Include_Test.jsp" flush="false"></jsp:include> <h1>include 2</h1> </body> </html>
-
Include_Test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>include test</title> </head> <body> <h1>include 3</h1> </body> </html>
-
μ€νκ²°κ³Ό
-
μ‘μ νκ·Έ - forward
-
μ€νμ€μΈ JSPνμ΄μ§μ μ μ΄ νλ¦μ νΉμ ν λ€λ₯Έ νμ΄μ§λ‘ λκΈ°κ³ μ ν λ μ¬μ©
-
μ΄λλ νμ΄μ§μ urlμ΄ μλ μ΄μ νμ΄μ§μ urlμ΄ λ³΄μ
-
redirectμλ λ€λ₯΄κ² μ§μ νλΌλ―Έν°λ₯Ό λκΈΈ μ μμΌλ©°, urlμ μ¨κΈΈ μ μλ€.
-
ex1)
-
Main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>λ©μΈνμ΄μ§</title> </head> <body> <h1>λ©μΈνμ΄μ§</h1> <% System.out.println("sub.jspλ‘ μ΄λ"); %> <jsp:forward page="Sub_Forward.jsp"></jsp:forward> </body> </html>
-
Sub_Forward.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>μλΈνμ΄μ§</title> </head> <body> <h1>μλΈνμ΄μ§</h1> </body> </html>
-
μ€νκ²°κ³Ό
-
-
ex2) forward + form
-
Forward_Form.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>forward / form</title> <style> table { border: 1px solid black; padding: 10px; } </style> </head> <body> <form action="Forward_Form_Test01.jsp" method="post"> <input type="hidden" name="forwardPage" value="Forward_Form_Test02.jsp"> <table> <tr> <td>μ΄λ¦</td> <td><input type="text" name="name"></td> </tr> <tr> <td>λμ΄</td> <td><input type="text" name="age"></td> </tr> <tr> <td>μ£Όμ</td> <td><input type="text" name="addr"></td> </tr> </table> <input type="submit" value="μ μ‘"> </form> </body> </html>
-
Forward_Form_Test01.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>κ²°κ³Ό 01</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); %> <% System.out.println("test01 νμ΄μ§ -> test02 νμ΄μ§ μ΄λ"); %> <jsp:forward page="Forward_Form_Test02.jsp"></jsp:forward> <%-- <jsp:forward page='<%=request.getParameter("forwardPage") %>'></jsp:forward> --%> </body> </html>
-
Forward_Form_Test02.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>κ²°κ³Ό 02</title> <style> table tr td{ border: 1px solid black; padding: 10px;s } </style> </head> <body> <% request.setCharacterEncoding("UTF-8"); %> <table> <tr> <td>μ΄λ¦</td> <td><%=request.getParameter("name") %></td> </tr> <tr> <td>λμ΄</td> <td><%=request.getParameter("age") %></td> </tr> <tr> <td>μ£Όμ</td> <td><%=request.getParameter("addr") %></td> </tr> </table> </body> </html>
-
μ€νκ²°κ³Ό
-
μ‘μ νκ·Έ - param
-
forward, include νκ·Έμ λ°μ΄ν° μ λ¬μ λͺ©μ μΌλ‘ μ¬μ©λλ νκ·Έ > λ¨λ μΌλ‘ μ¬μ©λμ§ λͺ»νκ³
<jsp:forward>
λ<jsp:include>
νκ·Έ λ΄λΆμ μ¬μ©λ¨ -
μ΄λ¦(name)κ³Ό κ°(value)λ‘ μ΄λ£¨μ΄μ Έ μμ
-
ex1) forward + param
-
Forward_Param.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>forward / param</title> </head> <body> <% System.out.println("forward/param κ²°κ³Ό νμ΄μ§λ‘ μ΄λ"); %> <jsp:forward page="Forward_Param_Test.jsp"> <jsp:param value="param_value" name="value"/> <jsp:param value="param_name" name="name"/> </jsp:forward> </body> </html>
-
Forward_Param_Test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>forward / param κ²°κ³Ό νμΈ</title> </head> <body> <%! String param_value; String param_name; %> <% param_value = request.getParameter("value"); param_name = request.getParameter("name"); %> value : <%=param_value %> <br> name : <%=param_name %> </body> </html>
-
μ€νκ²°κ³Ό
-
-
ex2) include + param
-
Include_Param.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>include / param</title> </head> <body> <h1>include 1</h1> <jsp:include page="Include_Param_Test.jsp" flush="false"> <jsp:param value="param_value" name="value"/> <jsp:param value="param_name" name="name"/> </jsp:include> <h1>include 2</h1> </body> </html>
-
Include_Param_Test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("UTF-8"); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>include / param test</title> </head> <body> <h1>include 3</h1> <% String value = request.getParameter("value"); String name = request.getParameter("name"); %> value : <%=value %> <br> name : <%=name %> </body> </html>
-
μ€νκ²°κ³Ό
-