Show TOC

DirectivesLocate this document in the navigation structure

Use

You use directives to pass information to the Web Container. They do not generate output and are independent of user requests.

Features

Directives have the following general syntax:

Code Syntax
               <%@ directive {attribute="value"}* %>
            

There are six types of directives (the last three directives are used only in tag files, see Developing Tag Files ):

  • page

    The page directive is executed at translation time. You use It to define page-dependent properties for the whole JSP page, such as buffer size, session preferences, character encoding, and so on. The syntax of the page directive is <%@ page page_directive_attr_list %> .

    For more information, see Using the page Directive .

    Sample Code
                         <%@ page pageEncoding="UTF-8" contentType="text/html;UTF-8" import="java.util.*,java.io.*" %>
    This page directive imports the java.util and java.io packages. It also defines that the JSP file is saved in UTF-8 (the page encoding attribute) and that the response will contain Content-type header with the respective value.
    
                      
  • include

    The include directive is also executed at translation time. You use it to define resources to be included in your original JSP. These resources may be static ones, such as HTML or XML, or another JSP page. The syntax of the include directive is <%@ include file="relativeURL" %> .

    Note

    In contrast to the other JSP directives, the include directive may appear more than once and at any position in the JSP. The resulting JSP will contain all content and logic supplied by the original JSP and the included resources in the order they are supplied in the original JSP.

  • taglib

    You use the taglib directive to import tag libraries into a JSP. The syntax of the taglib directive is <%@ taglib ( uri="tagLibraryURI" | tagdir="tagDir" ) prefix="tagPrefix" %>.

    Sample Code
                         To import JSTL [Page 79] tag libraries use:
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
    
                      

    Taglib directives are not avaialble in JSP documents. You define custom tag libraries using the namespace declaration in the root tag.

    Sample Code
                         <jsp:root
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:jsp="http://java.sun.com/JSP/Page"
    version="2.1">
    …
    </jsp:root>
    
                      
  • tag

    The tag directive is similar to the page directive, but applies to tag files instead of JSP files. You could think of it as a tag descriptor in the TLD. In this directive you can define the tag's body-content, description, small/large icon, display name, example, if the tag can accept dynamic attributes and other properties similarly to the page directive: isELIgnored, pageEncoding, deferredSyntaxAllowedAsLiteral, trimDirectiveWhitespaces, import, language.

    Sample Code
                         <%@ tag display-name="Addition"
    body-content="scriptless"
    dynamic-attributes="dyn"
    small-icon="/WEB-INF/sample-small.jpg"
    large-icon="/WEB-INF/sample-large.jpg"
    description="Sample usage of tag directive" %>
    
                      
  • attribute

    The attribute directive is analogous to the <attribute> element in the Tag Library Descriptor, and allows for the declaration of custom action attributes.

    Sample Code
                         <%@ attribute name="x" required="true" fragment="false"
    rtexprvalue="false" type="java.lang.Integer"
    description="The first operand" %>
    
                      
  • variable

    The variable directive is analogous to the <variable> element in the Tag Library Descriptor, and defines the details of a variable exposed by the tag handler to the calling page.

    Sample Code
                         <%@ variable name-given="sum"
    variable-class="java.lang.Integer"
    scope="NESTED"
    declare="true"
    description="The sum of the two operands" %>