beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Include Directive in JSP

By Chaitanya Singh | Filed Under: JSP tutorial

Include directive is used for merging external files to the current JSP page during translation phase (The phase where JSP gets converted into the equivalent Servlet).

Why we need to use the 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 practice. Let’s understand this with the help of an example – I have 100 external files and 1 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 – Suppose 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.

The above two reasons can be considered as advantages of using include directive.

Syntax:

This is the syntax of include directive in JSP.

<%@ include file="URL of the file" %>

We must need to specify relative URL –

If file is in the same folder where the current JSP page resides then we can just mention the file name else the relative path to the file needs to be specified.

Include Directive Example

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-example-output

Let us know if you have any questions regarding it.

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
  • Declaration tag
  • Expression tag
  • Scriptlets
  • Directives
  • Include Directive
  • Param Directive
  • Exception handling
  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty
  • Implicit Objects
  • Session implicit object
  • Validate session
  • Request implicit object
  • Response implicit object
  • Out implicit object
  • Application implicit object
  • Config implicit object
  • pageContext implicit object
  • Exception implicit object
  • Expression language
  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap