BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Include Directive in JSP

Last Updated: July 27, 2022 by Chaitanya Singh | Filed Under: JSP tutorial

In this tutorial, you will learn include directive in JSP with example. Include directive is used to copy the content of one page to another. It’s like including the code of one file into another. The include directive includes the original content of the included resource at page translation time (The phase where JSP gets converted into the equivalent Servlet).

Why we need to use the include directive?

You may be wondering why we need to use include directive, can’t we simply add the file’s content in the current JSP instead of using the directive?

We can copy the content of external file and paste it in the main JSP, however it would not be a good programming practice. Let’s understand this with the help of an example – I have 100 external files and one main JSP file. If I just copy the content of all files in the main JSP then I have to edit it whenever there is a change in any of the external file, instead we can include all files using directive and edit the particular file whenever needed.

Also, by using include directive you can enhance the code re-usability: Let’s say there is a certain code or data which needs to be there in all the JSP pages of your application then you can simply have that code/data in one file and include the file in all the JSP pages.

These mentioned points also shows the advantages of using include directive.

Syntax of Include Directive

<%@include file ="value"%>

Here, value is the JSP file name which needs to be included. If the file is in the same directory then just specify the file name otherwise complete URL(or path) needs to be mentioned in the value field.

JSP include directive Example

In this example, we have three jsp pages: file1.jsp, file2.jsp and index.jsp. We have included the content of file1.jsp and file2.jsp in the index.jsp using include directive.

index.jsp

<html>
<head>
<title>Main JSP Page</title>
</head>
<body>
<%@ include file="file1.jsp" %>
Main JSP Page: Content between two include directives.
<%@ include file="file2.jsp" %>
</body>
</html>

file1.jsp

<p align="center">
This is my File1.jsp and I will include it in index.jsp using include directive
</p>

file2.jsp

<p align="center">
This is File2.jsp
</p>

output: The output would look like this when you run the above code. As you can see we have included the content of file1 and file2 in out main JSP page with the help of include directive.

Include Directive in JSP
Also read:
JSP include directive vs include tag

❮ JSP directivesJSP exception handling ❯

Top Related Articles:

  1. Java Server Pages (JSP) Life Cycle
  2. JSP Scriptlet Tag (Scripting elements)
  3. JSP forward action tag
  4. JSP Implicit Objects
  5. Difference between include directive and include tag in JSP

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. vyshnavi says

    June 3, 2015 at 9:47 AM

    how do v validate data typed by a user in a registration page? is it possible using jsp ?
    can i include java script in jsp ?

    Reply
    • Ram says

      August 6, 2015 at 10:12 AM

      Yes, you can write javascript inside jsp file. Like HTML file.

      Reply
  2. Siddharth says

    March 15, 2016 at 9:48 AM

    what type of file included in the .jsp file

    Reply

Leave a Reply Cancel reply

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

JSP Tutorial

  • Basics of JSP
  • Life cycle of JSP
  • JSP in Eclipse IDE

Scripting Elements

  • Scriptlet Tag
  • Expression tag
  • Declaration tag

Implicit Objects

  • Implicit Objects
  • JSP Request
  • JSP Response
  • JSP Config
  • JSP Application
  • JSP Session
  • JSP Out
  • JSP pageContext
  • JSP Exception
  • Validate session

JSP directives

  • JSP Directives
  • Include Directive

JSP Exception

  • Exception handling

JSP Action

  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty

Expression language

  • Expression language

JSP Custom Tags

  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap