Recall, aside from HTML, there are three types of JSP structures.
|Types of JSP structures|
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" … %>
<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.
- The Page directive defines attributes and characteristics of a JSP page.
- The Include directive can be used to break a single JSP into multiple files.
- The Taglib directive allows the JSP to import a tag library.
- In this chapter, Page and Include directives are explored.
- The Taglib directive, which is used to import or specify a tag library to be used, is covered in the Tags chapter.
The Page directive defines details about/for the whole JSP page. The chart below lists the available page directive attributes.
|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"|
|import||Class and/or package names||None||import="java.net.URL"|
|buffer||Buffer size, or false||"8kb"||buffer="12kb"|
The page import directive allows imports to be added to the generated servlet.
<%@ page import=”java.util.*” %>
<%@ page import=”java.io.*” %>
The resulting servlet generated from the JSP above would include lines of code to import the java.util.* and java.io.* classes.
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.
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.