In this section, we will in 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 difference between them.
How JSP (Java Server Pages) works
Before discussing about include directive or include action its worth remembering that how JSP pages works insideservlet container. When a request comes for JSP page, that JSP page first translated into Servlet class and thenthat servlet gets compiled and loaded into heap memory by the web container. Code written using JSP declaration e.g. <%!> goes into body of generated servlet as instance variable and all code from body goes into service() method of generated Servlet. Incase of tomcat web-server these generated servlet files goes inside work directory. In short JSP pages has threesteps 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
Include directive in JSP is used to import a static file or dynamic file e.g. JSP inside another JSP. Most commonexample of include directive is including header and footer in JSP. Include directive is also called file includebecause 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++. file attribute is usedspecify resource to be included and can be specified in either relative URL or absolute URL. Since resourceincluded using include directive is included during translation time and jsp doesn't compile if there is anychange in included file. So include directive is not suitable to include dynamic resources because if includedfile changes between multiple request only old value is used always. include action is better choice for includingdynamic resource which we will see in next section.
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="loan.jsp" %>
here contents of loan.jsp will be included during request time instead of translation time and any change inloan.jsp will reflect immediately. Due to this nature include action is also called dynamic include in JSP.It also referred as page includes because of page attribute used to specify 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 popular servlet jsp interview question and mostly asked to Junior or mid-senior Java programmersduring 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 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 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 including action use page attribute for the same purpose.
5. Another difference suggested by N. Satish babu in comment section is that 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.