JSP: Directive Elements

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

In this article I am going to discuss about directive elements of JSP and their attributes.

JSP directives are used to add or remove some behavior like making the current page as error page, set the content type, add 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. Lets discuss about these directives.

include Directive

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

For example, if we want to include “anotherfile.jsp” into “example.jsp” we need to include the following line into “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 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 into 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 setting the buffer size to 4kb.

  • autoFlush attribute
    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 “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”. 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 “autoFlush” value to “false”; there is no way to flushing 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 “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 JSP page as an error page we need to set “isErroPage” attribute value to “true”.
  • extends attribute
    Generally every JSP page is turned into a servlet; we call that servlet as ”JSP equivalent servlet”. That servlet is extended from 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 importing packages. We can import any multiple packages by separating them with comma (“,”).
    <%@ page import="java.util.*,java.io.*" %>
  • info attribute is used to describe about the servlet and we can retrieve this information by calling 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”; whichmeans expression language is enabled in every JSP page by default. Whatever we write inside expression language that is dynamically calculated and 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 below result.

    ${89+67}
  • isThreadSafe attribute
    By default all servlets and JSP accepts 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. 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 one 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. Default value of this attribute is “false”.
    <%@page trimDirectiveWhitespaces="true" %>

taglib Directive
Tag library contains set of tags. If you want to use any tag from 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

2 comments for “JSP: Directive Elements

Leave a Reply