Directives in JSP (original) (raw)

JSP directives are special instructions in a JSP page that guide the web container during the translation phase. They help control page behavior, include resources, and use external tag libraries.

**Syntax :

<%@ directive attribute = "value"%>

Directives can have a number of attributes that you can list down as key-value pairs and separate by commas. The blanks between the @ symbol and the directive name, and between the last attribute and the closing %>, are optional.

**Different types of JSP directives

There are three different JSP directives available. They are as follows:

1. Page Directive

JSP page directive is used to define the properties applying the JSP page, such as the size of the allocated buffer, imported packages, and classes/interfaces, defining what type of page it is, etc.

**Syntax:

<%@page attribute = "value"%>

**Different properties/attributes

The following are the different properties that can be defined using page directive :

**1. import: This tells the container what packages/classes are needed to be imported into the program.

**Syntax:

<%@page import = "value"%>

**Example :

html `

<%-- JSP code to demonstrate how to use page directive to import a package --%>

<%@page import = "java.util.Date"%> <%Date d = new Date();%> <%=d%>

`

**Output:

**2 . contentType: This defines the format of data that is being exchanged between the client and the server. It does the same thing as the setContentType method in servlet used to.

**Syntax:

<%@page contentType="value"%>

**Usage Example:

html `

<%-- JSP code to demonstrate how to use page directive to set the type of content --%>

<%@page contentType = "text/html" %> <% = "This is sparta" %>

`

**Output :

**3. info: Defines the string which can be printed using the 'getServletInfo()' method.

**Syntax:

<%@page info="value"%>

**Usage Example:

html `

<%-- JSP code to demonstrate how to use page directive to set the page information --%>

<%@page contentType = "text/html" %> <% = getServletInfo()%>

`

**Output:

**4. buffer: Defines the size of the buffer that is allocated for handling the JSP page. The size is defined in Kilo Bytes.

**Syntax:

<%@page buffer = "size in kb"%>

**5. isELIgnored: This attribute tells if the page supports expression language. By default, it is set to false. If set to true, it will disable expression language.

**Syntax:

<%@page isElIgnored = "true/false"%>

**Usage Example:

html `

<%-- JSP code to demonstrate how to use page directive to ignore expression language --%>

<%@page contentType = "text/html" %> <%@page isELIgnored = "true"%>

`

**Output:
(blank page)

**6. errorPage: Defines which page to redirect to, in case the current page encounters an exception.

**Syntax:

<%@page errorPage = "true/false"%>

**Usage Example:

html `

//JSP code to divide two numbers <%@ page errorPage = "error.jsp" %>

<%
// dividing the numbers int z = 1/0;

// result out.print("division of numbers is: " + z);
%>

`

**7. isErrorPage: It classifies whether the page is an error page or not. By classifying a page as an error page, it can use the implicit object 'exception' which can be used to display exceptions that have occurred.

**Syntax:

<%@page isErrorPage="true/false"%>

**Example:

html `

//JSP code for error page, which displays the exception <%@ page isErrorPage = "true" %>

Exception caught

The exception is: <% = exception %> Output:

`

**Output:

2. Include Directive

JSP include directive is used to include other files into the current jsp page. These files can be html files, other sp files etc. The advantage of using an include directive is that it allows code re-usability.

**Syntax:

<%@include file = "file location"%>

**Example: In the following code, we're including the contents of an HTML file into a jsp page.
**a.html

html `

This is the content of a.html

`

**index.jsp

html `

<% = Local content%> <%@include file = "a.html"%> <% = local content%>

`

**Output :

3. Taglib Directive

The Taglib directive in JSP is used to declare a tag library that contains custom tags. It allows developers to use predefined tags (like JSTL) instead of writing Java code inside JSP pages.

**Syntax:

<%@taglib uri = "library url" prefix="the prefix to
identify the tags of this library with"%>

**Example:

html `

<%-- JSP code to demonstrate taglib directive--%> <%@ taglib uri = "http://www.oracle.com/technetwork/java/index.html" prefix = "c" %>

<c:out value = "${'This is Sparta'}"/>

`

The taglib directive is used to link a tag library (like JSTL) so custom tags can be used instead of Java code. The prefix (e.g., c) identifies and accesses these tags in the JSP page.

**Output: