CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

JSP – Directive Elements

There are three types of elements in JSP; Directive elements, Scripting elements, and Action elements.

In this article, we are going to discuss the Directive elements in JSP and their attributes.

JSP directives are used to add or remove some behavior; like making the current page an error page, setting the content type, adding buffering capability, etc.,.

A directive can be attached to a JSP page in two ways:

HTML syntax:

<%@directivename attributes%>

XML syntax:

<jsp:directive.directivename attributes%>

There are three types of directive tags in JSP; include directive, page directive, and taglib directive. Let us discuss these directives.

include Directive

  • This directive is used to include the source code of one page into another page.
  • This type of including is also called compile-time including or static including.
  • At translation time container first includes the source code of one page to another and then translates that page into a servlet.
  • Include directive has a single attribute called “file”.

For example, if we want to include “anotherfile.jsp” into “example.jsp” we need to include the following line into the “example.jsp” file.

<%@include file="anotherfile.jsp"%>

We can include any number of JSP pages into the destination JSP page, but the destination page must be either “.html” or “.jsp” format.

page Directive

Page directive provides many attributes that can be applied to JSP pages. See the below image those are all the possible attributes that can be applied to the page directive. Now we will discuss each of those.

JSP - Page directive - Attributes
JSP – Page directive – Attributes
  • buffer attribute is used to increase or decrease or disable the buffer size. The response generated by the JspWriter can be stored in the buffer and can flush it to the browser. Through this, we can achieve high performance. By default buffer size is 8kb.
    <%@page buffer="none"%>

    is used to disable the buffer.

    <%@page buffer="4kb"%>

    is used to set the buffer size to 4kb.

  • autoFlush attribute
    the default value of this attribute is “true” which means when the buffer is full, the response in the buffer will be automatically flushed to the browser. If we set the “autoFlush” value to “false” we need to manually flush the buffer; otherwise, we will get the “java.io.IOException: Error: JSP Buffer overflow” exception. Remember that when the buffer is disabled with

    <%@page buffer="none"%>

    we should not set “autoFlush” to “false”. The below statement

    <%@page autoFlush="false"%>

    will cause the error. Because after setting buffer value to “none”; whatever the response that is generated will immediately flush to the browser, but if we set the “autoFlush” value to “false”; there is no way to flush the response; hence we will get an error.

  • contentType attribute is used to set the content type and character set of the response. character set values are defined by IANA(Internet Assigned Numbers Authority). Some of the values for English are below:

    ISO-8859-1, ISO-8859-15, windows-1252, US-ASCII, UTF-8

    These values are not case-sensitive. You can write them in uppercase or lowercase. The default value of “contentType” attribute is “text/html”. The default value of the “charset” attribute is “ISO-8859-1”.

    <%@ page contentType="text/html; charset=UTF-8" %>
  • errorPage attribute
    If we set any URI as a value to the errorPage attribute, any error occurred in the current page, the corresponding JSP page will be displayed.

    <%@page errorPage="errorpage.jsp"%>
  • isErroPage attribute
    All JSP pages are not able to work as error pages. The default value of “isErrorPage” attribute is “false”. If we want to set the JSP page as an error page we need to set the “isErroPage” attribute value to “true”.
  • extends attribute
    Generally, every JSP page is turned into a servlet; we call that servlet”JSP equivalent servlet”. That servlet is extended from a particular class defined by the server vendor. For example, in Tomcat every JSP equivalent servlet is extended from “HttpJspBase”. “HttpJspBase” is extended from “HttpServlet”. If you want to extend your JSP equivalent servlet from other than “HttpJspBase” you can specify your own class as follows:

    <%@page extends="com.shilpa.codesteps.MySuperClass"%>
  • import attribute is used to import packages. We can import multiple packages by separating them with a comma (“,”).
    <%@ page import="java.util.*,java.io.*" %>
  • info attribute is used to describe the servlet and we can retrieve this information by calling the getServletInfo() method. For example the below code;
    <%@ page info="this JSP is used to know about info attribute" %>
    <%
       out.println(getServletInfo());
    %>

    will produce the below output:

    this JSP is used to know about info attribute

  • isELIgnored attribute
    The default value of this attribute is “false”; which means expression language is enabled in every JSP page by default. Whatever we write inside expression language is dynamically calculated and a response will be generated. We can write expressions within “${” and “}”. If this attribute is set to “true”, that means expression language is disabled, the expressions are not evaluated and the same expression text will be returned as a response. For example, if write the expression

    ${89+67}

    in the JSP file, the browser will evaluate the expression and it will display the value

    156

    But if we disable expression language using the statement

    <%@page isELIgnored="true" %>

    the browser will display the expression as it is; which will produce the below result.

    ${89+67}
  • isThreadSafe attribute
    By default, all servlets and JSP accept multiple requests at a time. That means they are by default thread-safe. The default value of “isThreadSafe” attribute is “true”. If you want to restrict this behavior we can make “isThreadsafe” value to “false”; so, the JSP equivalent servlet will be implemented by SingleThreadModel.

    <%@ page isThreadSafe="false" %>
  • session attribute is used to enable or disable the session. By default session is enabled i.e., session default values is “true”. To disable the session use the following:
    <%@ page session="false" %>
  • pageEncloding attribute is used to set the encoding type of the response. The default value of this attribute is “ISO-8859-1”. We can set the “pageEncoding” value like below:
    <%@ page pageEncoding="UTF-8" %>
  • language attribute is used to set the language used in the current JSP. The only possible value for this attribute is “java”.
    <%@ page language="java" %>
  • trimDirectiveWhitespaces attribute is used to remove extra white spaces in the response. Generally, it is used to reduce the size of the response. The default value of this attribute is “false”.
    <%@page trimDirectiveWhitespaces="true" %>

taglib Directive
Tag library contains a set of tags. If you want to use any tag from a particular tag library; we have to define that tag library using “taglib” directive as follows:

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>

Thank You

JSP – Directive Elements

3 thoughts on “JSP – Directive Elements

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top