
JSP Directives are elements of a JSP source code that instruct the server how to process a page.
Table of Contents
Types of JSP Directives
There are following types of JSP directives:
- Page Directive
- Include Directive
- Taglib Directive
Syntax of JSP Directives
<%@ directive attribute = "value"%>
JSP Directives – Page, Include and TagLib
Let us discuss about JSP directives one by one with syntax and give examples for each directive. In this section we discuss the basic concept of page, includes and taglib directive and attributes of page directive.
JSP Page Directive
The page directive lets you control the structure of the servlet by importing classes, customizing the servlet superclass, setting the content type and so on.
You can use a page directive can be placed anywhere within the document. Page directive provides instructions to a container, the container applies the instruction to the whole JSP.
Syntax of Page Directive
<%@ page [attribute-list] %> <%@ page attribute = "value" %>
Important Attributes of Page Directive
There are following attributes of page directive:
- autoFlush
- buffer
- contentType
- errorPage
- extends
- import
- info
- isELIgnored
- isErrorPage
- isThreadSafe
- language
- pageEncoding
- session
autoFlush Attribute
When the output buffer is full it should be flushed automatically or if the buffer overflows it should raise an exception. The autoflush attribute handles such cases. In other words, autoFlush controls the behavior of the servlet output buffer. By default autoFlush=”true”.
<%@ page autoFlush="true/false" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" autoFlush="false" %>
buffer Attribute
The buffer attribute specifies the size of the buffer that the out variable uses, which is of type JspWriter. Servers can use a larger buffer than you specify, but not a smaller one.
By using buffer attribute you can define the size of buffering and default size is 8KB.
<%@ page page buffer="sizekb" %> <%@ page page buffer="none" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" buffer="16KB" %>
contentType Attribute
The ContentType attribute sets the Content-Type response header, which indicates the MIME type of the document the client is receiving.
<%@ page contentType="MIME-Type" %> <%@ page contentType="MIME-Type; charset=Character-Set" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" %>
errorPage Attribute
We use the errorPage attribute to define the error page, it helps to redirect to the error page when there is an exception on the page.
<%@ page errorPage="Relative URL" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" errorPage="page.jsp" %>
extends Attribute
You can use extends attribute to extend (inherit) the class similar as Java does.
<%@ page extends="package.class" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="java.util.*" %>
import Attribute
We use import attribute to import class, interface or all the members of a package.
<%@ page import="package.class" %> <%@ page import="package.class1,...,package.classN" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="java.util.*" %>
info Attribute
The info attribute defines a string that you can retrieve it from the servlet and you can have a ServletInfo() method to access it.
<%@ page info="Some Message" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" info="Welcome to Geekcer!" %>
isELIgnored Attribute
The isELIgnored attribute handles cases when you need to manage whether to ignore or evaluate JSP 2.0 Expression Language (EL) in general.
<%@ page isELIgnored="true/false" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false" %>
isErrorPage Attribute
The isErrorPage attribute indicates whether the current page can act as an error page for another JSP page.
<%@ page isErrorPage="true/false" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isErrorPage="true" %>
session Attribute
If you want the page to participate in an HTTP session, you should use the session attribute. By default session = “true”.
<%@ page session="true/false" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" session="false" %>
pageEncoding Attribute
The pageEncoding attribute defines the character encoding for JSP page.
<%@ page pageEncoding="value" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" %>
language Attribute
The language attribute specifies the scripting language which we use in JSP page.
<%@ page language="language-name" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" language="java" %>
isThreadSafe Attribute
The isThreadSafe attribute defines the thread safety in the page. By default isThreadSafe=”true”.
<%@ page isThreadSafe="true/false" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isThreadSafe="false" %>
JSP Include Directive
You can include directive when you need to insert file in JSP page. The file content can be a JSP file, an HTML file, or a text file.
You should include the file at the point in which you want the file.
Syntax of Include Directive
<%@ include file="resource-Name" %>
<%@ 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>Insert title here</title>
</head>
<body>
<%@ include file="header.html" % >
<%@ include file="footer.html" % >
</body>
</html>
JSP Taglib Directive
By using Taglib directive you can use your user defined tag library into the JSP page. It uses a set of custom tags, identifies the location of the library.
Syntax of Taglib Directive
<%@ uri="uri" prefix="value" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="https://geekcer.com/" prefix="geekTag" %>
<!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>Insert title here</title>
</head>
<body>
</body>
</html>