You are on page 1of 8

JAVA SERVER PAGES

Directives
Directives
 Directives are instructions from the JSP to the
container that can be used to set the page properties,
to import Java classes and packages, and to include
external web pages and custom tag libraries.

 The three directives are:


 Page
 Include
 taglib
Page directive
The page directive starts with <%@ page and will be applied
during the servlet-generation process only to the current
page. It’s used with such attributes as import, extends,
session, errorPage, and contentType.
For example, an equivalent of the Java import statement
looks like this:
<%@ page import=”java.io.*” %>
With servlets, set the type of the output by code like
response.setContentType(“text/html”).
The JSP version of this code is shown here:
<%@ page contentType=”text/html” %>
Now consider the following example:
Import attribute in page directive
<%@page import="java.util.Date" %>
<head>
<title>Page Directive - import</title>
</head>
<body> <a>Date is:</a>
<%= new java.util.Date() %>
</body>
</html>
The include directive
 Include—The include directive adds modularity to JSP
by allowing to include the contents of external pages
in JSP.
 JSP "include directive“ is used to include one file to the
another file. This included file can be HTML, JSP, text
files, etc.
 It is also useful in creating templates with the user
views and break the pages into header footer and
sidebar actions.
 It includes file during translation phase
Include directive – Example program
<%@ include file="directive_header_jsp3.jsp" %>
<html>
<head>
<title>Include Directive </title>
</head>
<body>
<a>This is the main file</a>
</body>
</html>
directive_header_jsp3.jsp
<html>
<body>
<a>Header file : </a>
<%int count =1; count++;
out.println(count);%> :
</body>
</html>
Taglib directive
• Taglib—the Taglib directive is used for Custom Tag
Libraries. This directive allows the JSP page to use
custom tags written in Java.
• Custom tag definitions are usually defined in a
separate file called as Tag Library Descriptor.
• For the current JSP to use a custom tag, it needs to
import the tag library file which is why this directive is
used.
• Following is how taglib directive is used.
• <%@ taglib uri=”location of definition file”
prefix=”prefix name” %>

You might also like