❮ Previous Next ❯

JSP Include Directive

JSP Include directive is used to copy the content of one JSP page to another. It's like including the code of one file into another.

Syntax

<%@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.

Note: It can be used anywhere in the page.

Example:

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

You can use the above code in your JSP page to copy the content of home.jsp file. However in this case both the JSP files must be in the same directory. If the home.jsp is in the different directory then instead of just file name you would need to specify the complete path in above code.

JSP 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).

include_directive

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 200 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.

Due to above this two reasons we require to use include directive.

Let's see an example for better clarity:

index.jsp

<html>
<head>
</head>
<body>
<%@ include file="home1.jsp" %>
Welcome to Silan Software
<%@ include file="home2.jsp" %>
</body>
</html>

home1.jsp

<html>
<head>
</head>
<body>
<p align="center">
Java means java8s.com and java8s.com means Java
</p>
</body>
</html>

home2.jsp

<html>
<head>
</head>
<body>
<p align="center">
Welcome to java8s.com
</p>
</body>
</html>

Output


❮ Previous Next ❯


Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved