In this post, I will help you understand the differences between JSP include action and JSP include directive.

You know, in JSP, there are two include mechanisms which look like they do the same thing:

<%@ include file="content.jsp" %>

And:

<jsp:include page="content.jsp" />

The former is called include directive and the latter is called include action. Both are used to include a resource into the current JSP page, which is useful for re-using common pages and code fragments across JSP pages such as header, footer, menu, category… of a web application.

However they behave differently, the following table summarizes the commons and differences of these two include mechanisms:

 

Content type

Include mechanism

Inclusion time

Page URL

Position-sensitive

Include directive

static

Source code of the included file.

at translation time

Relative URL.

Runtime expression not allowed.

Yes

Include action

static and dynamic

Response of the included page.

at request time

Relative URL.

Accept runtime expression

Yes

 

The include directive <%@include %>:

  • Use this include mechanism for static content like HTML or JSP whose content does not change at runtime.
  • Because the inclusion happens at translation time, and the included page’s code is merged as it is into the current JSP page, thus the following characteristics:
    • The included page should not be a complete, standalone JSP page. Instead, it should be a code fragment which constitutes a part of the including page.
    • The included page can access scriptlet variables declared in the parent page, because they are actually one page when merged.
    • It takes only one request-response cycle when running a JSP page which includes static pages using this <%@include %>; directive.
    • Changes to the included page may not get updated in the parent page immediately, depending on version of JSP/Servlet container.

 

The include action <jsp:include>:

  • Use this include mechanism for dynamic content like JSP or Servlet whose content may change at runtime.
  • Because the inclusion happens at request time, and response of the included page is merged into the parent page, this include mechanism has the following characteristics:
    • The included page must be a complete, standalone JSP or Servlet and it is executed as a separate servlet (JSP is finally compiled to servlet).
    • Each included page needs a complete request-response cycle, so if the parent page includes many pages using this <jsp:include> action, performance may downgrade.
    • The included page cannot access variables declared in the parent page, so we should pass variables explicitly if needed (using <jsp:param> is a way).
    • Since the included page’s URL can be a runtime expression, it’s possible to include pages conditionally at runtime.

       

Related JSP Tutorials:

 

Other JSP Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment