JSP Page Directive


The JSP directives are special JSP messages that tells the web container how to translate a JSP page into the servlet.

There are three types of directives - page directive,include directive, taglib directive

<%@ directive attribute="value" %>

A. JSP Page Directive :

  1. The Page directive used to define a set of attributes that apply to an complete JSP page
  2. Page directives can be placed anywhere in your JSP page.
  3. Generally, Page directives are written at the top of the JSP page.

Syntax of Page Directive :

<%@ page attribute="value" %>

XML equivalent of the above syntax as follows :

<jsp:directive.page attribute="value" />

B. List of Page Directive Attributes :

AttributePurpose
bufferSpecifies a buffering model for the output stream.
autoFlushControls the behavior of the servlet output buffer.
contentTypeDefines the character encoding scheme.
errorPageDefines the URL of another JSP that reports on Java unchecked runtime exceptions.
isErrorPageIndicates if this JSP page is a URL specified by another JSP page's errorPage attribute.
extendsSpecifies a superclass that the generated servlet must extend
importSpecifies a list of packages or classes for use in the JSP as the Java import statement does for Java classes.
infoDefines a string that can be accessed with the servlet's getServletInfo() method.
isThreadSafeDefines the threading model for the generated servlet.
languageDefines the programming language used in the JSP page.
sessionSpecifies whether or not the JSP page participates in HTTP sessions
isELIgnoredSpecifies whether or not EL expression within the JSP page will be ignored.
isScriptingEnabledDetermines if scripting elements are allowed for use.

C. Examples of Page Directives

1. Import Page Directive

  1. The import page directive attribute is used to import java packages.
  2. The value of import option is the name of the package you want to import.

In order to import java.sql.* use the following page directive:

<%@ page import="java.sql.*" %>

To import multiple packages comma is used as separator -

<%@ page import="java.sql.*,java.util.*"  %>

Following Packages are imported by default in the JSP -

java.lang.*
javax.servlet.*
javax.servlet.jsp.*
javax.servlet.http.*.

Example of the import Directive :

<html>  
<body>  
<%@ page import="java.util.Date" %>  
Today is : <%= new Date() %>  
</body>  
</html>

Output :

Today is: Sun Apr 13 12:03:01 IST 2014

2. Buffer Page Directive Attribute :

  1. The buffer attribute sets the buffer size in kilobytes in order to handle output generated by the JSP page.
  2. The buffer attribute is used to provide buffering ability to the server’s output response object.
  3. Servlet will be immediately directed to the response object if we specify the buffer attribute value to “none”
  4. <%@ page buffer="none" %>
    
  5. Below statement is used to direct the servlet to write output to a buffer of size not less than 8 kilobytes
  6. The default size of the buffer is 8Kb
Buffer ParameterExplanation
noneTo direct the servlet to write output directly to the response output object
8kbTo direct the servlet to write output to a buffer of size not less than 8 kilobytes
16kbTo direct the servlet to write output to a buffer of size not less than 16 kilobytes


3. autoFlush Attribute:

autoFlash ValueMeaning
autoFlush='false'Causes the servlet to throw an exception when the servlet's output buffer is full
autoFlush='true'Buffer will be flushed automatically when servlet's output buffer is full

The buffer and autoFlush attributes are written on a single page directive as follows -

<%@ page buffer="16kb" autoflush="true" %>

4. The errorPage Attribute:

  1. The errorPage attribute is used to tell JSP engine which page to display if there is an error while processing JSP.
  2. The value of the errorPage attribute is a relative URL.
  3. The errorPage attribute is used to display alternate page when JSP throws uncaught exceptions
<%@ page errorPage="error.jsp" %>

5. isErrorPage Attribute:

  1. isErrorPage attribute is used to indicate that the current JSP is used as the error page for another JSP.
  2. Value of isErrorPage can be either true or false.
  3. Default value of the isErrorPage attribute is false.

For example, the handleErrorMsg.jsp page sets the isErrorPage attribute to true because it is used to handle errors:

<%@ page isErrorPage="true" %>

6. extends Attribute:

  1. generated servlet will extend the class specified in the extends attribute.
  2. extends attribute specifies a superclass which must be extended by specified class.

For example, the following directive directs the JSP translator to generate the servlet such that the servlet extends somePackage.SomeClass:

<%@ page extends="somePackage.SomeClass" %>

7. info Attribute:

The info attribute lets you provide a description of the JSP. following is a coding example:

<%@ page info="This JSP Page Written By ZARA"  %>

8. isThreadSafe Attribute:

  1. isThreadSafe option marks a page as being thread-safe.
  2. By default, all JSPs are considered thread-safe.

If you set the isThreadSafe option to false, the JSP engine makes sure that only one thread at a time is executing your JSP. The following page directive sets the isThreadSafe option to false:

<%@ page isThreadSafe="false"  %>

9. language Attribute:

language attribute indicates the programming language used in scripting the JSP page.

<%@ page language="java" %>

10. session Attribute:

  1. Session attribute used to indicate whether JSP page uses HTTP sessions or not.
  2. If session attribute value is True then JSP page has access to a built-in session object
  3. If session attribute value is False then JSP page cannot access the built-in session object.
  4. Methods used for accessing session - session.getCreationTime() or session.getLastAccessTime()
<%@ page session="true" %>

11. isELIgnored Attribute:

  1. isELIgnored attribute is used to disable the evaluation of Expression Language (EL) expressions.
  2. Default value of the attribute is true, meaning that expressions, ${…}, are evaluated as dictated by the JSP specification.
  3. If the attribute is set to false, then expressions are not evaluated.
<%@ page isELIgnored="false" %>

12. isScriptingEnabled Attribute:

  1. isScriptingEnabled attribute used to determine whether scripting elements are allowed or not.
  2. the default value (true) enables scriptlets, expressions, and declarations.
  3. If the attribute’s value is set to false, a translation-time error will be raised if the JSP uses any scriptlets, expressions (non-EL), or declarations.
  4. You can set this value to false if you want to restrict usage of scriptlets, expressions (non-EL), or declarations:
<%@ page isScriptingEnabled="false" %>

13. The contentType Attribute:

It tells us what will be the content of the generated page.

<%@ page contentType="text/xml" %>

above line will tell that “Hey browser, Please render the page generated by JSP as XML document”.

contentType ValueMeaning
text/xmlDirects the browser to render the generated page as XML
text/htmlDirects the browser to render the generated page as HTML
application/mswordDirects the browser to render the generated page as Microsoft Word document
text/html:charset=ISO-8859-1Directs the browser to render the generated page as HTML with ISO Latin 1 encoding