beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Difference between include directive and include tag in JSP

By Chaitanya Singh | Filed Under: JSP tutorial

Include directive and include action tag both are used for including a file to the current JSP page. However there is a difference in the way they include file. Before I explain the difference between them, let’s have a brief about each one.

JSP Include Directive

index.jsp

<html>
<head>
<title>JSP include Directive example</title>
</head>
<body>
<%@ include file="display.jsp" %>
</body>
</html>

display.jsp

<p>This is the content of my file</p>

Output:

include-directive-example

JSP Include Action tag

index.jsp

<html>
<head>
<title>JSP include Action example</title>
</head>
<body>
<jsp:include page="display.jsp" />
</body>
</html>

display.jsp

same as above example

Output: It is exactly same what we got in include directive example.

include-action-example

JSP include directive vs include action tag

We have seen above that the output got from both is same, however there are few noticeable differences among them.

1) Include directive includes the file at translation time (the phase of JSP life cycle where the JSP gets converted into the equivalent servlet) whereas the include action includes the file at runtime.

2) If the included file is changed but not the JSP which is including it then the changes will reflect only when we use include action tag. The changes will not reflect if you are using include directive as the JSP is not changed so it will not be translated (during this phase only the  file gets included when using directive) for request processing and hence the changes will not reflect.

3) Syntax difference: Include directive: <%@ include file="file_name" %> whereas include action has like this <jsp:include page="file_name" />

4) When using include action tag we can also pass the parameters to the included page by using param action tag but in case of include directive it’s not possible.

<jsp:include page="file_name" />
 <jsp:param name="parameter_name" value="parameter_value" />
 </jsp:include>

These are the major differences between include directive and include action. If you have any questions regarding the topic, feel free to ask by dropping a comment below. Happy learning!!

Enjoyed this post? Try these related posts

  1. JSP Declaration tag – JSP Tutorial
  2. How to configure Apache Tomcat Server in Eclipse IDE
  3. How to validate and invalidate session in JSP
  4. JSP Expression Tag – JSP Tutorial
  5. jsp:useBean, jsp:setProperty and jsp:getProperty Action Tags
  6. How to fix Dynamic Web Project missing in Eclipse issue

JSP Tutorial

  • Basics of JSP
  • Life cycle of JSP
  • JSP in Eclipse IDE
  • Declaration tag
  • Expression tag
  • Scriptlets
  • Directives
  • Include Directive
  • Param Directive
  • Exception handling
  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty
  • Implicit Objects
  • Session implicit object
  • Validate session
  • Request implicit object
  • Response implicit object
  • Out implicit object
  • Application implicit object
  • Config implicit object
  • pageContext implicit object
  • Exception implicit object
  • Expression language
  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap