JSP 액션 태그 (JSP Action Tags)
<jsp:forward> 태그는 클라이언트 요청을 다른 JSP 페이지, 서블릿 또는 HTML 페이지로 전달합니다. 이 태그를 사용하면 서버 측에서 요청을 다른 리소스로 넘길 수 있습니다.
예제:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Forward Example</title>
</head>
<body>
<jsp:forward page="target.jsp" />
</body>
</html>
위 예제는 target.jsp로 요청을 전달합니다. 클라이언트는 URL 변화를 인지하지 못합니다.
<jsp:include> 태그는 다른 JSP 페이지나 HTML 파일의 콘텐츠를 현재 페이지에 포함시킵니다. 이 태그는 요청 시점에 포함 파일을 삽입합니다.
예제:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Include Example</title>
</head>
<body>
<jsp:include page="header.jsp" />
<div>
<p>This is the main content.</p>
</div>
<jsp:include page="footer.jsp" />
</body>
</html>
위 예제는 header.jsp와 footer.jsp를 현재 페이지에 포함시킵니다.
<jsp:param> 태그는 <jsp:forward> 또는 <jsp:include> 태그와 함께 사용되어, 요청에 파라미터를 추가합니다. 이 태그는 전달할 파라미터의 이름과 값을 지정합니다.
예제:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Param Example</title>
</head>
<body>
<jsp:forward page="target.jsp">
<jsp:param name="username" value="JohnDoe" />
<jsp:param name="age" value="25" />
</jsp:forward>
</body>
</html>
위 예제는 username과 age 파라미터를 target.jsp로 전달합니다.
, ,
<jsp:useBean> 태그는 JSP 페이지에서 자바 빈을 선언하고 인스턴스화합니다. id 속성은 빈의 이름을 지정하고, class 속성은 빈의 클래스 이름을 지정합니다.
<jsp:setProperty> 태그는 빈의 속성을 설정합니다. name 속성은 빈의 이름을 지정하고, property 속성은 설정할 속성의 이름을 지정합니다. value 속성은 설정할 값을 지정합니다.
<jsp:getProperty> 태그는 빈의 속성을 가져와 출력합니다. name 속성은 빈의 이름을 지정하고, property 속성은 가져올 속성의 이름을 지정합니다.
예제:
- Person.java
package com.example; public class Person { private String name; private int age;public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }} - index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>UseBean Example</title> </head> <body> <jsp:useBean id="person" class="com.example.Person" scope="page" /> <jsp:setProperty name="person" property="name" value="John Doe" /> <jsp:setProperty name="person" property="age" value="30" /><p>Name: <jsp:getProperty name="person" property="name" /></p> <p>Age: <jsp:getProperty name="person" property="age" /></p></body> </html>
위 예제는 Person 클래스를 사용하여 빈을 선언하고, 그 속성을 설정 및 출력합니다.
종합 예제 (Comprehensive Example)
모든 액션 태그를 종합적으로 사용하는 예제입니다.
- header.jsp
<div> <h1>This is the header</h1> </div> - footer.jsp
<div> <h1>This is the footer</h1> </div> - target.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Target Page</title> </head> <body> <h1>Target Page</h1> <p>Username: <%= request.getParameter("username") %></p> <p>Age: <%= request.getParameter("age") %></p> </body> </html> - index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Comprehensive Example</title> </head> <body> <jsp:useBean id="person" class="com.example.Person" scope="page" /> <jsp:setProperty name="person" property="name" value="John Doe" /> <jsp:setProperty name="person" property="age" value="30" /><jsp:include page="header.jsp" /> <h1>Main Content</h1> <p>Name: <jsp:getProperty name="person" property="name" /></p> <p>Age: <jsp:getProperty name="person" property="age" /></p> <jsp:forward page="target.jsp"> <jsp:param name="username" value="<jsp:getProperty name='person' property='name' />" /> <jsp:param name="age" value="<jsp:getProperty name='person' property='age' />" /> </jsp:forward> <jsp:include page="footer.jsp" /></body> </html>
이 종합 예제는 JSP 액션 태그를 모두 사용하여 빈을 선언하고 속성을 설정하며, 다른 JSP 파일을 포함하고 요청을 전달하는 방법을 보여줍니다.
