Include the results of another page
Including the results of another page in a JSP can be achieved using two primary mechanisms:
1. Include Directive (<%@ include %>):
This is used for static inclusion, where the content of the included file is inserted at the translation phase. This means the inclusion happens before the JSP page is compiled into a servlet.
2. Include Action (<jsp:include />):
This is used for dynamic inclusion, where the content of the included file is inserted at request time. This means the inclusion happens every time the page is requested.
Table of Contents
Static Inclusion with Include Directive
Example of Include Directive (<%@ include %>)
main.jsp:
jsp
<%@ include file="header.jsp" %>
<html>
<body>
<h1>Main Content</h1>
<%@ include file="footer.jsp" %>
</body>
</html>
header.jsp:
jsp
<h1>This is the header</h1>
footer.jsp:
jsp
<h1>This is the footer</h1>
Dynamic Inclusion with Include Action
Example of Include Action (<jsp:include />)
main.jsp:
jsp
<jsp:include page="header.jsp" />
<html>
<body>
<h1>Main Content</h1>
<jsp:include page="footer.jsp" />
</body>
</html>
header.jsp:
jsp
<h1>This is the header</h1>
footer.jsp:
jsp
<h1>This is the footer</h1>
Explanation Include the results of another page
- 1. Include Directive (<%@ include %>):
- In main.jsp, the header.jsp and footer.jsp files are included statically.
- The content of header.jsp and footer.jsp is merged into main.jsp at the translation phase, before the JSP is compiled into a servlet.
- 2. Include Action (<jsp:include />):
- In main.jsp, the header.jsp and footer.jsp files are included dynamically.
- Each time main.jsp is requested, it dynamically includes the output of header.jsp and footer.jsp at runtime.