JSP Directives and Page Directives

Intertech Tutorials

JSP Directives

Recall, aside from HTML, there are three types of JSP structures.

Types of JSP structures
Scripting elements
Directives
Tags

In addition, as of the JSP 2.0 specification, there is an additional expression language that Java JSP developers must know. In this chapter, JSP Directives are covered.

Directives define details of the JSP page. Specifically, directives provide instructions to the JSP engine on how to process the page. Directives are similar to C/C++ #pragmas. Directives have a syntax like this:


<%@ directiveName atribute1="value1" atribute2="value2" … %>
or
<jsp:directive.directiveName atribute1="value1" …/>

Conventionally, directives are specified at the top of a JSP page before any other JSP tags.

There are only a few types of directives.

Page Directive

The Page directive defines details about/for the whole JSP page. The chart below lists the available page directive attributes.

Attribute Value Default Examples
info Text string None info="Registration form"
language Scripting language name "java" language="java"
contentType MIME type, character set "text/html" contentType="text/html" charset=IOS-8859-1"
extends Class name None extends-"com.SomeJSP"
import Class and/or package names None import="java.net.URL"
session Boolean flag "true" session="true"
buffer Buffer size, or false "8kb" buffer="12kb"
autoFlush Boolean flag "true" autoFlush="false"
isThreadSafe Boolean flag "true" isThreadSafe="true"
errorPage local URL None errorPage="results/failed.jsp"
isErrorPage Boolean flag "false" isErrorPage="false"

The page import directive allows imports to be added to the generated servlet.


<%@ page import=”java.util.*” %>
<%@ page import=”java.io.*” %>

<html><body>hi</body></html>

The resulting servlet generated from the JSP above would include lines of code to import the java.util.* and java.io.* classes.


import java.util.*;
import java.io.*;

Sessions are automatically generated by every JSP, unless you turn session handling off using session attribute with the page directive.


<%@ page session=”false” %>

The content type attribute sets the content type for the JSP response.


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

If you recall, setting the content type had to be done by hand in a servlet. In servlet code, in the doGet or doPost method, the response object would be used to set the content type.


response.setContentType(“text/xml”);

In a JSP, the default content type is text/html so most JSP pages don’t need to use this directive.

The language attribute specifies the scripting languages to be used (in scriptlets, declarations, and expressions) on the page.


<%@ page language=”java” %>

In JSP 1.2, the only allowed value is java and it is the default value.

The extends attribute allows the Java superclass for the generated servlet to be specified. When specifying the superclass, the fully qualified name of the superclass must be provided. Sun recommends to “use this attribute cautiously”

“as it can limit the JSP container's ability to provide a specialized superclass that improves the quality of the compiled class.”


Copyright (c) 2008-2013. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.