JSP Include Directive

Include Directive:

  1. This directive is used to include a file while translation phase.
  2. This directive tells the container to merge the contents of the JSP file given in the include directive to the current JSP which is being handled.
  3. This can be coded anywhere in the JSP page.
  4. Then the source JSP page is converted into a java servlet class.
  5. The included file can be a static resource or a JSP page.
  6. If several JSP’s say 100 requires some common code it is recommended to separate that common code in a separate file where ever it is required we have to place one include call.

 

Syntax of Include Directive:

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

 

If the JSP file which you are trying to use is in another project inside your application it’s better to give the relative URL for accessing that JSP file.

Advantages:

  1. It promotes reusability.
  2. Enhancement will become very easy.
  3. It improves maintainability.

 

Disadvantages:

The JSP compilation procedure is that, the source JSP page gets compiled only if that page has changed. If there is a change in the included JSP file, the source JSP file will not be compiled and therefore the modification will not get reflected in the output

 

This can be understood by means of the below simple program:

Let’s make use of three JSP files, one include.jsp which is the main JSP, jsp1.jsp and jsp2.jsp which will be included in the include.jsp file as below.

include.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 

<%@ include file="jsp1.jsp" %>
<p align="center">
Main JSP Page: The data written between two directives.
<%@ include file="jsp2.jsp" %>
</body>
</html>

 

jsp1.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<p align="center">
This is the first JSP file.</p>
</body>
</html>

 

jsp2.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<p align="center">
This is the second JSP File.</p>
</body>
</html>

When we run the include.jsp file then we will get the below output as expected:

IncludeDirective

Posted on July 12, 2014 in Java Server Pages

Share the Story

Response (1)

  1. Angela
    February 18, 2016 at 4:43 am · Reply

    Whoever wrote this, you know how to make a good arlecti.

Leave a reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top