The following article describes the Difference Between include Directive and include Action in JSP.
In JSP (JavaServer Pages), both of these are the mechanisms to include content from other resources in a JSP page. However, they differ in how they include the content and when they are processed.
The include directive (<%@ include file=”filename.jsp” %>) is a static include that includes the contents of another JSP file at compile time. The included content becomes part of the JSP page’s source code, and the resulting page is a single, complete file that is served to the client at runtime. The included file can be any static content, such as HTML, JSP, or text files.
On the other hand, the include action (<jsp:include page=”filename.jsp” />) is a dynamic include that includes the contents of another JSP file at runtime. The included content is processed separately and merged with the calling page’s content at runtime. The resulting page is a combination of the calling page’s content and the included content. The included file can be any dynamic content, such as JSP, servlets, or custom tags.
The following table shows a summary of the differences.
Include Directive | Include Action | |
Processing | Compile time | Runtime |
Included Content | Static | Dynamic |
Included File Type | Any static content (HTML, JSP, text, etc.) | Any dynamic content (JSP, servlets, custom tags, etc.) |
Resulting Page | Single, complete file | Combination of calling page and included content |
In general, you would use the include directive when you want to include static content that doesn’t change at runtime, such as header or footer files. You would use the include action when you want to include dynamic content that is generated at runtime, such as content from a servlet or custom tag.