include directive JSP Example

include directive JSP Example

JSP gives the facility of including the contents of HTML file or JSP file into another JSP file with a single statement of include directive JSP.

It is very easy to use and the same is achieved in Servlets with RequestDispatcher. It is simply equivalent to copying the code of one JSP file into another JSP file. Let us see an example.

How include directive JSP works?

The code of included JSP page is copied and pasted in the code of including JSP file. The combined source code is translated to a Servlet, compiled, executed and send output of execution as response to client.

Figurative explanation of include directive JSP

include directive JSP

Example on include directive JSP

The application consists of 2 JSP files, 1 HTML file and one client (HTML) program (to enter the name of employee). The application is meant to give an Experience certificate to an employee. For any employee, header (company name) and footer (company information) are common and never changes. Only body changes where employee name and years of experience comes.

1. Experience.html: This takes employee name from client and sends to Certificate.jsp.

2. Certificate.jsp: This includes the contents of Header.html and Footer.jsp.

3. Header.html: This gives company name and address as title to a certificate.

4. Footer.jsp: This gives the footer of date and company Web site name etc.

1. File Name: Experience.html. This takes employee name and sends to Certificate.jsp.

2. File Name: Certificate.jsp. This issues the certificate by taking data from Header.html and Footer.jsp.

3. File Name: Header.html. This adds company name and address to the certificate.

4. File Name: Footer.jsp. This adds date and company site name.

Screenshot of HTML file Experience.html when entered with employee name


Screenshot of Certificate.jsp


The Certificate.jsp includes the content of Header.html and Footer.jsp. These files data included with the following include directive.

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

1. Salient Points with include directive JSP

  1. The include directive can be used anywhere in the JSP page.
  2. While the JSP file is being converted into Servlet, the code of included JSP/HTML file is pasted into including JSP file. For all the JSPs involved, at translation time, only one Servlet is generated (there is a circumstance where multiple Servlets are generated).
  3. The included file can be static code HTML file (like Header.html) or dynamic code JSP file (like Footer.jsp).
  4. Here, in the above example, all files are in the same Web server directory. If located in different locations, write the file with directory structure.
  5. include directive works at translation phase.
  6. XML equivalent syntax is <jsp:include page="relative url" />

2. Advantages of include directive JSP

  1. Best for including the common content in multiple files just with simple include call.
  2. Increases reusability.
  3. Modifications become easy and changing the code in a single included file affects all including files.
  4. Maintenance and debugging becomes easy.

Here, only one Servlet is created. But in include action, the same job is done, but creating with two different Servlets and is explained clearly in JSP include action Example.

Leave a Reply

Your email address will not be published. Required fields are marked *