Difference between include directive and include action in JSP Servlet

Both include directive and include action is used to include output of a static page e.g. html or a dynamic page e.g. jsp inside another jsp file.

Include action vs Include directive in JSP

What is include directive in JSP


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="detail.jsp" %>

The code written in detail.jsp will be included as it is in the jsp file which included it during JSP translation time and then merged code is get compiled to generate Servlet which is later used for server request. Include directive is also refereed as static import and it acts like #include from C or C++. file attribute is used to 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 request only old value is used always. include action is better choice for including dynamic resource.

What is include action in JSP

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="detail.jsp" %>

here contents of detail.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 as page include because of page attribute used to specify included resource.

Difference between include directive and include action in JSP

1. Resource included by include directive is loaded during jsp translation time while resource included by include action is loaded during request time.
2. Any change on included resource will not be visible in case of include directive until jsp file compiles
again. While in case of include again any change in included resource will be visible in next request.
3. include directive is static import while include action is dynamic import
4. include directive uses file attribute to specify resource to be included while include action use page attribute for same purpose.
5. Value of the attribute to  JSP include action can be  dynamic and request time expression and you can also pass parameter to the file which you are going to include using include action e.g.

<jsp:include page="header.jsp">
<jsp:param name="nav" value="home"/>
</jsp:include>

Comments