JSP Directives: Page, Include & Taglib Directive

In the previous tutorial, we have learned 9 implicit objects in JSP. In this tutorial, we will learn about JSP Directives with Example. Basically, there are 3 types of JSP Directives, Page directive, include directive, and taglib directive. We will learn these directives in detail. But first, understand what is JSP Directives?

What are JSP Directives?

JSP Directives are configured how the container generates the java code while creating the servlet. To set global values within a particular JSP Page, directives are typically used, therefore they affect the entire page. The role of the directives is to pass the information to the JSP container. Therefore, a directive doesn't produce any output to the current output stream of the JSP Page. The JSP directives have the following common syntax:

                                  <%@ directive { attr = "value" } %>

We can use the following three Directives.
1) Page Directive.
2) include Directive.
3) taglib Directive.

Also Read: 9 Implicit Objects in JSP

1) Page Directive: 

To define the attribute for the JSP Page, the page directive provides a mechanism. Page directive is used to set the content type of the page as well as the language that will be used for scripting.

The syntax of the JSP Page directive:

syntax of page directive

Attributes of Page directives.

There are 11 attributes of JSP Page Directives these are discussed in detail below.

i) language: 

It defines the scripting languages to be used in scriptlets, expression, and declarations.

ii) pageEncoding: 

It defines the character encoding for the JSP Page.if any other no specified, The default value is ISO-8859-1.

iii) isErrorPage: 

 It indicates if this JSP Page is intended to be the URL Target of another JSP Page errorPage. if set to true, then the implicit script language variable exception is available and references the Throwable object from the offending JSP page. If this value is false, then the exception variable isn't available and will cause a translation error if it is referenced.

jsp page directive

iv) isThreadSafe: 

It tells the container whether the page is thread-safe with proper synchronization implemented on the page. A value of true indicates that the container may send multiple requests to the page. if the value is false,          requests will be queued and sent one at a time.

v) import: 

It has the same meaning as the import statement in java classes.

jsp page directive

vi) info:  

It is used to set an informational String that's incorporated into the page and accessible by using Servlet.getServletInfo() method.

jsp page directive

vii) extends:

It provides a mechanism to define a fully qualified class name of the superclass that the container will extend to generate the translated page.

viii) content type:

It defines the character encoding for the JSP Page and the Multipurpose Internet Mail Extension(MIME) types for the response of the JSP page.

jsp page directive

ix) buffer: 

It specifies the buffering model for writing content to the output stream. The value of none indicates to write directly through to the ServletResponse using its PrintWriter.While specifying a buffer size, it must be in kilobytes(kb).

jsp page directive

x) autoflush: 

It specifies that the buffered output is flushed or not and the default value of "autoFlush" attribute is true. When set to true, the output buffer is automatically flushed when the buffer is full. when set to false, this will raise an exception when the buffer is full.

Read: What is Exception in Java?

xi) session: 

 It specifies, whether this page participates in the HttpSession, .if set to true, then the implicit object session is available for access. This object is of type javax.Servlet.http.HttpSession. if set to false, the object isn't available and attempted access to it will cause an error

2. include directive: 

The include directive tells the container to substitute the defined resource content inline to the JSP page. This can either be template text or code. In the include directive, the resources specified must be a relative  URL, that's accessible by the container. if the include files changes, then it doesn't necessarily trigger a recompilation of the JSP file.if an include file changes, then the JSP 2.1 specification recompilation hasn't required.

The syntax of JSP includes directive:  <@ include file = "resource name"%>

Example of JSP includes directive:

jsp include directive

3. taglib directive: 

The role of the taglib directive is very important as it allows a JSP page to work with custom tag libraries(taglib).In a JSP page, the taglib directive declares that the page uses a tag library, which uniquely identifies the tag libraries by using a URI and associated a tag prefix with the action in the library. It is also possible to have tag files in a directive called /WEB-INF/tags in JSP 2.1
The syntax of JSP taglib directive:
Syntax of JSP taglib directive

Example of JSP taglib directive:

jsp taglib directive

JSP Directives: Page, Include & Taglib Directive JSP Directives: Page, Include & Taglib Directive Reviewed by Prashant Srivastava on November 19, 2018 Rating: 5

No comments:

Powered by Blogger.