include Directive -


The include directives allows us to include content of other files (text, html, jsp etc) into our current JSP file at the time of translation of JSP file into corresponding servlet.
The include directive is useful when you have same code to be used by multiple JSP files, for example All JSP Pages in a application may have same header and footer, in that case instead of writing the same code on each page, we can define header and footer in separate files and then include them on each pages that require header and footer. So it reduces the time and effort.
It has the following syntax -
<%@ include file="relativeURL" %>        

Example -

In this example we will create a Header.html and Footer.html files that will contain header and footer of application, and we will include these files in JSP page using include directive.

1- Header.html - Create a Header.html file as follows -
<!DOCTYPE html>
<html>
    <head>
        <title>include directive demo</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <h1 style="text-align: center; padding: 20px">Welcome to GetTechNotes</h1>
        <hr>        

3- index.jsp - In index.jsp file we will include header and footer as follows -
<%@ include file="Header.html" %>
<h2>Todays date is - </h2> <%= new java.util.Date().toString()%>
<%@ include file="Footer.html" %>        
3- Footer.html - Create a Footer.html file as follows -
<div style="position: absolute; bottom: 0px; height: 20px; width: 99%; text-align: centre">
    copyright @ GetTechNotes.com
</div>
    </body>
</html>        

4- Now run the project and it will show you the following output -



Download this example (NetBeans implementation) - Click Here


taglib Directive -

The taglib Directive is used to define a tag library that provides a number of tags we can use in our jsp files. It has the following syntax -
<%@ taglib uri = "uri" prefix = "Tag_Prefix" >        
Where uri is the uniform resource identifier (location) of tag library file and Tag_Prefix is the prefix used with tags provided by tag library.

For Example -
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="corejava" %>
<corejava:catch></corejava:catch>    
It is a broad topic and we will cover it in detail in later tutorials.