Introduction
JSP directives convey messages to the container and are a part of how the page is compiled. A directive provides global information about an entire JSP page. With JSP directives, container code for translating JSP into servlet code is given special instructions.
The translation phase in the JSP life cycle involves converting JSP to a servlet. To perform certain aspects of JSP processing, they provide instructions to the container.
The key-value attributes can be added to directives by separating them with commas.
Directives are described in JSP by the *%@ %> tag. The directives @include @page and @taglib appear only on JSP pages.
Types of JSP Directives
There are three main types of JSP directives:
- @page
- @include
- @taglib
Let's learn about them in detail
1. @page
A page directive sets out page-specific attributes like scripting languages, error pages, and buffering requirements. They get applied across the entire JSP page. The directive gives instructions regarding the current JSP page to the container.
Syntax:
<% @page attribute_name=”value” attribute_name=”value”... %>
We can use multiple attributes with the @page directive. Some Attributes used in the @page are:
- Language = It specifies which language is used on the JSP page. The "Java" language is set as the attribute value by default.
<%@page language="language_name"%>
- Extends = It specifies a superclass that determines whether the generated servlet requires inheritance or not.
<%@page extends="Package_name.Class_name"%>
- Import = Packages are imported using this attribute. The attribute of the page directive is beneficial since it allows you to mention multiple packages at once separated by commas. You can also have multiple instances of page elements, each having its package.
<%@page import="package_name"%>
<%@page import="package_name"%>
Or
<%@page import="package_name, package_name"%>
- Session = When developing a user-interactive JSP application, we ensure that users have access to their data until the session is active. The session attribute must be true for a page to maintain a session. JSP pages use this attribute to handle HTTP sessions. It can be either true or false. As the session attribute defaults to true, a server may assume that this page requires an HTTP session if you do not specify it.
<%@page session="true or false"%>
- autoFlush = If this is true, the buffer should be flushed once it reaches full size. The exception will be thrown if the buffer exceeds its size.
<%@page autoFlush="true or false"%>
- contentType = JSP pages use this attribute to specify their content type. It specifies the MIME (Multipurpose Internet Mail Extension) type of the HTTP response, where the content type can be anything such as “text/Html,” “text/XML,” etc.
<%@page contentType="content_type"%>
- iserrorPage = In case of an exception, a page with the isErrorPage attribute will be called by another page. The isErrorPage Page attribute specifies the URL of an error response. It handles exceptions that are not handled by the page.
<%@page isErrorPage="true or false"%>
- Info = By setting this attribute, you set the information of the JSP page, which is later retrieved by the getServletInfo() method of the Servlet interface.
<%@page info="Text_you_want_to_retrieve"%>
- isELIgnored = The Expression Language(EL) attribute specifies whether the expression will be evaluated or not on a JSP page. It is set to false by default.
<%@page isELIgnored="true or false"%>
- isThreadSafe = When isThreadSafe is set to true, the JSP page is thread-safe, i.e., multiple threads can execute the page simultaneously. HOWEVER, the JSP engine won't allow multithreading if it is set to false so that a single thread will run all the page code.
<%@page isThreadSafe="true or false"%>
2. @include
The include directive is used to merge external files into the current JSP page during translation. When translating JSP into Servlet, the translation phase occurs.
The include directive increases code reusability - If you have particular code or data that needs to be in all JSP pages of your application, you can put the code/data in a file and include it in all the JSP pages.
The attribute used in @include is a file.
Syntax:
<% @include file=” filename or URL of the file”%>
The file_name refers to the JSP file that needs to be included in this case. You can specify the file name if the file is located in the same folder. Otherwise, you must have the complete URL(or path) in the value field.
Example:
<html>
<head>
<title>@include directive example</title>
</head>
<body>
<%@ include file="file_name1.jsp" %>
Main JSP Page: Content between two include directives.
<%@ include file="file_name2.jsp" %>
</body>
</html>
3. @taglib
It implements a tag library prefix of "taglib" with the JSP taglib directive. A tag library is defined by the JSP taglib directive, which establishes many tags at once. The tags are described in the TLD (Tag Library Descriptor) file. It will be helpful to learn this tag in the custom tag section.
Your JSP page uses it to identify the location of the library and the custom tags.
Syntax:
<%@taglib uri ="taglib_for_URI" prefix="tag prefix"%>
The Attributes used in @taglib are:
- uri = uri is a uniform resource locator used to determine the location of custom tags.
- Prefix = Tag prefix will be a string used to identify custom tags in the location specified by URI.
Example:
<%@taglib uri="https://www.codingninjas.com/" prefix="example_tag" %>
<html>
<body>
<example_tag: Hello!!! Ninjas/>
</body>
</html>
You can also read about the JSP Architecture and Lifecycle.