Table of contents
1.
Introduction
2.
Types of JSP Directives
2.1.
1. @page
2.2.
2. @include
2.3.
3. @taglib
3.
Frequently Asked Questions
3.1.
How does JSP include directives and JSP include actions differ?
3.2.
How does a buffer set to "none" affect the system?
3.3.
What makes JSP better than JavaScript?
3.4.
How are JavaBeans and taglib directives different?
4.
Conclusion
Last Updated: Sep 1, 2024

Directives in JSP

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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:

  1. @page
  2. @include
  3. @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.

Frequently Asked Questions

How does JSP include directives and JSP include actions differ?

Include directives are static and processed when the JSP page is translated into its corresponding servlet. The file included on the page also contains the included file’s code at the time of translation and compilation. This directive is commonly used to have only static resources, such as banners, headers, and footers. If you change the included file, it will not affect the files that had included this file.

Include action is a dynamic action that is handled at runtime. A function call to the included file will be made at runtime. A child element of this include action element can pass additional parameters to this action. Whenever you change an included file, those files that use it will also be affected.

How does a buffer set to "none" affect the system?

A servlet's output is directed directly to the response output object if the buffer is set to "none.".

What makes JSP better than JavaScript?

It is impossible to access server-side resources like databases, catalogs, and pricing information with JavaScript. In HTML, a dynamic element cannot be embedded. JavaServer Pages(JSP) makes this possible. A small amount of dynamic data can be inserted quite easily.

How are JavaBeans and taglib directives different?

JavaBeans and taglib provided reusability for JavaBeans. But they differ in the following ways.

  • The JavaBeans are suitable for storing information and state, while taglibs generate presentation elements.
  • Use JavaBeans to present information and custom tags to implement actions.

Conclusion

By providing directives, you instruct the container on how some aspects of JSP processing should be handled. You can use three types of directives. In the page directive, instructions are provided to the container. The include directive instructs the container to merge the JSP content with the contents of other external files during the translation phase.  In the taglib, custom tags are used to identify the library's location and how they should be inserted into JSP pages.
 Recommended Readings:

Become more familiar with other areas of web development if you're new to it by learning from guided path, which offers all the courses free of cost.

Live masterclass