Java.Servlet.What is the difference between and and the <%@include %> directive?

📜 The Three Ways to Include Content in JSP

Feature<%@ include %><jsp:include><c:import>
TypeStatic includeDynamic includeDynamic import (even external URLs)
When it happensTranslation time (before JSP becomes a servlet)Request time (when user accesses page)Request time
What it can includeOnly local files (same web app)Only local files (same web app)Local files or external URLs
Syntax styleJSP directive (compile-time)JSP action tag (runtime)JSTL core tag (runtime)
Parameter passing❌ No✅ Yes (<jsp:param>)✅ Yes (<c:param>)

🔵 1. <%@ include file="..." %>Static Include

When?

  • Happens at JSP translation time (when the server compiles the JSP into a servlet).

What happens?

  • The contents of the included file are physically copied into the JSP page before compilation.
  • Single servlet is generated.

Use for:
✅ Common page parts like headers, footers, menus that rarely change.

Syntax:

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

Pros:

  • Simple and fast.
  • Included at compile time → better performance.

Cons:

  • Changes to included file require recompilation of the JSP.
  • Only includes files inside your app — no external URLs.

🔵 2. <jsp:include page="..." />Dynamic Include

When?

  • Happens at request time (when the user requests the page).

What happens?

  • The included resource is executed separately, and its output is injected into the response dynamically.
  • Still same request and response.

Use for:
✅ Including dynamic content that might change frequently (like latest news, user profile sections).

Syntax:

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

You can also pass parameters:

<jsp:include page="menu.jsp">
    <jsp:param name="theme" value="dark" />
</jsp:include>

Pros:

  • Always gets the fresh content of the included resource.
  • You can pass parameters easily.

Cons:

  • Slightly slower than static because it happens at runtime.
  • Only works within your application.

🔵 3. <c:import url="..." />Dynamic Import via JSTL

When?

  • Happens at request time (like <jsp:include>).

What happens?

  • Can import content from anywhere, even an external URL like https://example.com/news.
  • The fetched content is inserted into the current page or stored in a variable.

Use for:
✅ Importing dynamic content from internal or external sources.

Syntax:

<c:import url="/footer.jsp" />

or

<c:import url="https://news.example.com/latest" var="newsContent" />
${newsContent}

Pros:

  • Can import external pages or APIs.
  • Can store imported content in a variable (var attribute).

Cons:

  • Slower for external URLs (depends on external server response).
  • Need JSTL library.

🎯 Summary Table

Aspect<%@ include %><jsp:include><c:import>
When?Translation time (compile)Request time (runtime)Request time (runtime)
ScopeLocal fileLocal fileLocal or external URL
Recompile needed?Yes if included file changesNoNo
Pass parameters?❌ No✅ Yes (<jsp:param>)✅ Yes (<c:param>)
Syntax typeDirectiveJSP ActionJSTL Core tag

📢 Important Tip:

  • Use <%@ include %> when the included content is static and rarely changes (for example, a common header or footer).
  • Use <jsp:include> when you need dynamic behavior and you might need to pass parameters.
  • Use <c:import> when you need to fetch remote content (like from another server) or manage output into variables.

🚀 Real-World Example

If you have a header.jsp, you might:

  • Use <%@ include file="header.jsp" %> if the header is always the same.
  • Use <jsp:include page="header.jsp"/> if you want the header to depend on the current user.
  • Use <c:import> if the header needs to pull content from another site (e.g., news feed from a remote server).
This entry was posted in Без рубрики. Bookmark the permalink.