[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


include λ””λ ‰ν‹°λΈŒ νƒœκ·Έ <%@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>
      
    • μ‹€ν–‰κ²°κ³Ό

Categories:

JSP/Servlet