JSP include Directive

JSP include Directive


Posted in : Java Posted on : January 24, 2012 at 6:47 PM Comments : [ 0 ]

In this tutorial you will learn about how to use 'include' directive in JSP.

JSP include Directive

In this tutorial you will learn about how to use 'include' directive in JSP.

include directive element in JSP is used to include a static file in a Jsp file and inserts the contents of the included file in a jsp file at translation time when the jsp file is compiled. Following files can be included in a JSP file these are HTML, JSP or Text. In case of JSP file including, elements of JSP are parsed first and then includes the parsed result with the other text in the JSP file. Wherever you will use this (include) directive element in a calling JSP file it will add and placed there the included file parsed result and the processing will be resume from the next line of the calling JSP file.

Note : When you are including a file into JSP file using <% @ include %> it should be careful about the included file should not contained those tags that may cause to conflict with the same tag in calling JSP file such as <html></html>, <body></body>.

Syntax

<%@ include file="relativeURL" %>

Example :

includeDirective.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP include Directive Example</title>
</head>
<body>
<%! int i=5; 
String str= "Hi";
public String getString()
{
return "Bye";
}
%>
<table border= "1">
<tr><td>Value of Integer</td>
<td><% out.println(i);%></td>
</tr>
<tr>
<td>Value of String</td>
<td><% out.println(str); %></td>
</tr>
<tr>
<td>Value of method getString()</td>
<td><% out.println(getString()); %></td>
</tr>
</table>
<p>Following contents are the content of the file that are to be included.</p>
<%@include file= "includeToIncludeDirective.html" %>
</body>
</html>

Code of the jsp file that is to be included into the above jsp file.

includeToIncludeDirective.html

<ul>
<li><p>Contents of this file are belonging to 
<b>includeToIncludeDirective.html</b></p></li>
<li><p>This page is included by the <b>includeDirective.jsp</b></p></li>
<li><p>This is a static page.</p></li>
</ul>

Output :

When you will execute the  includeDirective.jsp file example then the output will be as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics