Difference between include directive and include action in JSP Servlet? Example

Difference between include directive and include action is one of the most popular JSP interview questions and also asked as What is the difference between page include and file include in JSP.  Similar to the difference between forward and send redirect and URL encoding vs URL rewriting, this is also asked at a beginner level. Here we will see page include vs file include in detail. Both include directive and include action is used to include the output of a static page e.g. HTML or a dynamic page like jsp inside another jsp file.

In this JSP tutorial, we will see what is include directive in JSP and what is include action, and the difference between include a directive and include action. This JSP interview question is mostly asked during 2-4 years of experience in Java J2EE. 

By the way, this article is in continuation of my other posts on J2EE interviews like Top 10 Spring Interview Questions and Top 10 Servlet Interview questions. If you are interested in Core java or multi-threading then you can check the Top 20 Core Java Interview questions.


Include action vs Include directive in JSP

In this section, we will detail how pages are included using include action and directive, how JSP pages are translated into Servlet, and how to use include action and directive along with a couple of differences between them.




How JSP (Java Server Pages) works

differences between page include and file include in JSPBefore discussing about include directive or include action its worth remembering that how JSP pages works inside servlet container. When a request comes for JSP page, that JSP page first translated into Servlet class and then that servlet gets compiled and loaded into heap memory by the web container. 

Code written using JSP declaration e.g. <%!&gt; goes into body of generated servlet as instance variable and all code from body goes into service() method of generated Servlet. 


In case of tomcat web-server these generated servlet files goes inside work directory. In short JSP pages has three steps to get executed translation-->Compilation -->Execution. Also JSP file will only be re-compiled only if there is a change in JSP file.

What is include directive in JSP? Example

Include directive in JSP is used to import a static file or dynamic file e.g. JSP inside another JSP. Most common example of include directive is including header and footer in JSP. Include directive is also called file include because resource to be included is specified using file attribute as shown in below example of include directive:

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

The code written in loan.jsp will be included as it is in the jsp file which included it during JSP translation time and than merged code is get compiled to generate Servlet which is later used to server request. Include directive is also refereed as static import and it acts like #include from C or C++. 

The file attribute is used specify resource to be included and can be specified in either relative URL or absolute URL. Since resource included using include directive is included during translation time and jsp doesn't compile if there is any change in included file. 


So include directive is not suitable to include dynamic resources because if included file changes between multiple requests only old value is used always. include action is a better choice for including dynamic resources which we will see in the next section.

What is include action in JSP? Example

Include action in JSP is another way of including a jsp file inside another jsp file. Included resource is specified using page attribute as shown in below example :

<jsp:include page="loan.jsp" %>

here contents of loan.jsp will be included during request time instead of translation time and any change in loan.jsp will reflect immediately. Due to this nature include action is also called dynamic include in JSP. It also referred to as page includes because of page attribute used to specify an included resource.

Difference between include directive and include action in JSP

include directive is <%@ include file="loan.jsp" %> and include action in JSP is <jsp:include page="loan.jsp" %>
As I said earlier this is one of the popular servlet jsp interview question and mostly asked Junior or mid-senior Java programmers during interviews. Following are some common differences between include action and include directive in JSP:

1. Resource included by including directive is loaded during jsp translation time while resource included by including action is loaded during request time.

2. Any change on an included resource will not be visible in case of include directive until jsp file compiles again. While in the case of include again any change is the included resource will be visible in the next request.

3. include directive is static import while include action is dynamic import

4. include directive uses file attribute to specify the resource to be included while including action use page attribute for the same purpose.

5.  Another difference suggested by N. Satish babu in the comment section is that value of the attribute to  JSP include action can be  dynamic and request time expression and you can also pass a parameter to the file which you are going to include using include action like

<jsp:include page="header.jsp">
    <jsp:param name="some" value="thing"/>
</jsp:include>


That’s all on this popular jsp interview difference between JSP include action and include directive. Now we know what does include directive means and when to use include action or include directive. 

Here are some more interview questions you may like.

P. S. - If you are new to Servlet and JSP then I also suggest you go through a comprehensive course to learn Servlet and JSP in depth. If you need a recommendation check out these best SErvlet and JSP courses from Udemy and Pluralsight for Java programmers.

13 comments :

Anonymous said...

Indeed page include and file include are better reference as include action and include directive in jsp. also you should better title it as difference between Include action and include directive in JSP because you are just doing that.

Java Tutorial said...

Also check out my tutorial on JSP include directive and JSP include action

Hemang said...

Hi, there is a flush attribute in action, Can you please let us know what is this flush attribute which is mandatory and what is difference if we keep flush attribute as true or false ?

Renu said...

Another problem with jsp page include or include directive is that it doesn't accept expression language or any jsp expression because its not evaluated at request time. recently I have to convert include directive into include action in J2EE web application so that header and footer jsp pages which is include on request time at checks for certain attributes on request and session scope.

Javin @ spring interview questions answers said...

@Renu thanks for pointing it out. since include directive is not processed as runtime or request time its logical than any expression e.g. EL will not be evaluated.

Anonymous said...

hi javin as per my knowledge the line "e.g. <%!> goes into init() method of servlet" is false, as the data included in these tags will go into the body of class not into init() method

N.satish babu said...

hi javin, there are lots of other differences between them, the value of the attribute to the jsp:include action tag can be any dynamic value i.e. request time attribute expression also, and also we can send the attributes to the including page using something like this,

<jsp:include page="1.jsp">
<jsp:param name="some" value="thing"/>
</jsp:include>

and the including page can be accessed it using request.getParameter("some") or using EL like ${param.some} as it will be stored in request scope

Javin @ sendredirect vs forward said...

@Anonymous, you are correct <%!> represent declaration in JSP and any variable declared using declarative tag in JSP is created as instance variable in generated Servlet. Best way to verify is look on servlet files generated by Tomcat on work directory. Thanks for pointing that out.

@N.satish babu, Thanks for sharing those important difference between include action and include directive in JSP. request time expression is great feature, similarly ability to pass parameters but most important difference is when they get included i.e. translation time vs request time.

teji said...

i included a file a.jsp and changed content after running the program once.and the change reflected in both jsp include directive and jsp include action.
kindly specify why

Unknown said...

@Diyyana Kishore Babu, another basic and important difference is that, in case of include directive the content of the include file(s) will copy to service method so there is only one service method of one Servlet class to generate. When come to Page directive, there are two Servlet classes will generate with two difference service methods. The first service method output in the form of response object will copy to second service method response object. -- Diyyana Kishore Babu

Anonymous said...

when I change the page that is include.. those changes are reflecting with include directive why so ?

Unknown said...

What is advantage of using one over other ?

Anonymous said...

Can i include a servlet.class with <%@ include file="servlet.class" %> ?
With <jsp:include page="servlet.class" i can.

Post a Comment