Difference include directive vs include action JSP


Knowing the difference between JSP include directive and JSP include action is very important to a Programmer in terms of performance and for a fresher for interviews.

Basically both are used to include HTML static page contents or a dynamic JSP page contents in another JSP file.

We know in JSP Architecture how a JSP file is executed by the JSP container. First JSP file is translated to Servlet source file, then compiled, executed and output is sent to client as response.

The difference comes mainly in how the included file contents are placed in including file. It is very important to know when to use what, else performance will be lost.

1. First let us see how include directive (also known as "file include" as file attribute is used) works:

File Name: Certificate.jsp


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


In the above code, Certificate.jsp is called as including file and Footer.jsp is known as included file. While the Certificate.jsp file is translated to Servlet source file (of .java extension), the Footer.jsp file contents are pasted as it is in Certificate.jsp where <%@ inlcude .. %> tag comes. Then, the Certificate.jsp is translated to Servlet source file, compiled to a .class file and then executed. For both JSP files, only one Servlet file is created.

What is the minus point here. If the code calls the statement <%@ include file="Footer.jsp" %> in two places in different parts of Certificate.jsp code, in both places the Footer.jsp content is pasted. It is okay, but see here what happens. By the time execution comes to the second statement, if the Footer.jsp contents are changed, the second statement also gives the old data only but not changed data of Footer.jsp because the content is pasted only once in two places. So, it is not suitable for included files that are very dynamic in nature and changing very often like hit count files, share value files etc. "include directive" is best suitable for static files (like advertisement banners, company headers and footers etc.) that do not change the data frequently. But by performance it is faster.

2. Now let us see how include action (also known as "dynamic include" or "page include" as page attribute is used) works:

File Name: Certificate.jsp


  


It is another style of including the contents of one JSP file in another. Here, two Servlet source files, one for each JSP, are created and compiled. Two Servlet .class files are generated. At the time of execution, the output of Footer.jsp is put in the output of Certificate.jsp and total output goes to client as response.

Now think of which is advantageous. It depends on the application needs. If the included file (Footer.jsp) changes very often, it is the best style to use. If the Footer.jsp changes, by the time control comes to the second statement, it is again compiled, executed and latest output is pasted in Certificate.jsp.

It can also be used to include pages that cannot be determined until runtime.

Let us explain through figuratively.

include directive vs include action JSP

include directive vs include action JSP

include directive vs include action JSP 11 Differences are tabulated.
Property include directive include action
Code syntax <%@ include file="Footer.jsp" %> <jsp:include page="Footer.jsp" />
Output binding Static binding Dynamic binding
Other Names Knwon as file include Known as page include
Nature of files Preferred when included page does not change often Preferred when included page changes often
No. of Servlets created Only one Servlet is created for multiple JSPs involved Multiple Servlets are created one for each JSP involved
Nature of Import It is static import It is dynamic import
Included file Loaded at compile time Loaded at runtime
Changes visible in included file Not visible until next compilation Visible in the next request
Attribute used file attribute is used page attribute is used
Execution action Execution done at translation time Execution done at request time
Performance Faster Comparatively slower
    Differences on other Topics

  1. 11 Differences between JSP include directive and include action
  2. 10 differences between include action and forward action in JSP
  3. Difference between JSP forward and redirect Table

JSP View All

Leave a Comment

Your email address will not be published.