Include Directive

The include directive includes the file into current JSP file when translation phase is going on. The include directive is used to tell the container to merge all the contents of the external file to the current file. The directives can be inlucded anywhere in the JSP page. The following is the syntax of the include directive:

SYNTAX:

<%@ include file = “filename. jsp” %>

In the above syntax the filename is the name of any file which is going to be included in the current JSP page in the body of which this statement is used.

 

Example of include directive:

Consider the following example in which we have a JSP page and inserted another JSP file into that page:

first.jsp

< html >

<head>

<title>First JSP Page</title>

</head>

<body>

<%@ include file = “asd.jsp” %>

</body>

</html>

asd.jsp:

< html >

< body >

< img src = “image.jpg” alt = “My first JSP page” / >

</ body >

</ html >

In the above example we have created a JSP page and its name is first.jsp. In this page we have copied the content of another JSP page named asd.jsp. In the page asd.jsp we have inserted an image and an alternate text for the image which wil be inserted in the first. jsp page.

Suppose we have to create several JSP pages in which we have to insert a header and footer. Then we can create a JSP page which will contain both header and footer and other functions that are used repeatedly and this page then can be copied to any of the JSP pages to use the functions. In this way we can use the include directive and is useful when you want to give certain representation to your web application.