Directives are messages to the JSP container that enable the programmer to specify page settings (e.g., the error page to invoke if an error occurs), to include content from other resources and to specify custom-tag libraries for use in a JSP. Directives (delimited by <%@ and %>) are processed at translation time. Thus, directives do not produce any immediate output, because they are processed before the JSP accepts any requests. Figure 27.17 summarizes the three directive types. These directives are discussed in the next several subsections.
Directive |
Description |
---|---|
page |
Defines page settings for the JSP container to process. |
include |
Causes the JSP container to perform a translation-time insertion of another resource's content. As the JSP is translated into a servlet and compiled, the referenced file replaces the include directive and is translated as if it were originally part of the JSP. |
taglib |
Allows programmers to use new tags from tag libraries that encapsulate more complex functionality and simplify the coding of a JSP. |
27.7.1. page Directive
The page directive specifies global settings for the JSP in the JSP container. There can be many page directives, provided that there is only one occurrence of each attribute. The only exception is the import attribute, which can be used repeatedly to import Java packages used in the JSP. Figure 27.18 summarizes the attributes of the page directive.
Attribute |
Description |
---|---|
language |
The scripting language used in the JSP. Currently, the only valid value for this attribute is java. |
extends |
Specifies the class from which the translated JSP can inherit. This attribute must be a fully qualified class name. |
import |
Specifies a comma-separated list of fully qualified type names and/or packages that will be used in the current JSP. When the scripting language is java, the default import list is java.lang.*, javax.servlet.*, javax.servlet.jsp.* and javax.servlet.http.*. If multiple import properties are specified, the package names are placed in a list by the container. |
session |
Specifies whether the page participates in a session. The values for this attribute are true (participates in a sessionthe default) or false (does not participate in a session). When the page is part of a session, implicit object session is available for use in the page. Otherwise, session is not available, and using session in the scripting code results in a translation-time error. |
buffer |
Specifies the size of the output buffer used with the implicit object out. The value of this attribute can be none for no buffering or a value such as 8kb (the default buffer size). The JSP specification indicates that the buffer used must be at least the size specified. |
autoFlush |
When set to TRue (the default), this attribute indicates that the output buffer used with implicit object out should be flushed automatically when the buffer fills. If set to false, an exception occurs if the buffer overflows. This attribute's value must be TRue if the buffer attribute is set to none. |
isThreadSafe |
Specifies whether the page is thread safe. If true (the default), the page is considered to be thread safe, and it can process multiple requests at the same time. If false, the servlet that represents the page implements interface java.lang.SingleThreadModel and only one request can be processed by that JSP at a time. The JSP standard allows multiple instances of a JSP to exist for JSPs that are not thread safe. This enables the container to handle requests more efficiently. However, this does not guarantee that resources shared across JSP instances are accessed in a thread-safe manner. |
info |
Specifies an information string that describes the page. This string is returned by the getServletInfo method of the servlet that represents the translated JSP. This method can be invoked through the JSP's implicit page object. |
errorPage |
Any exceptions in the current page that are not caught are sent to the error page for processing. The error-page implicit object exception references the original exception. |
isErrorPage |
Specifies whether the current page is an error page that will be invoked in response to an error on another page. If the attribute value is true, the implicit object exception is created and references the original exception that occurred. If false (the default), any use of the exception object in the page results in a translation-time error. |
contentType |
Specifies the MIME type of the data in the response to the client. The default type is text/html. |
pageEncoding |
Specifies the character encoding of the JSP page. The default value is ISO-8859-1. |
isELIgnored |
Specifies whether JSP container should evaluate expressions that use the Expression Language (EL)a new feature in JSP 2.0 that allows JSP authors to create scriptless JSP pages. EL is typically used with JSP tag libraries, which are beyond the scope of this book. An EL expression has the form ${exp}. If the attribute value is true, the EL expressions are ignored, which means that the JSP container does not evaluate the expressions at translation time. If false, the EL expressions are evaluated by the JSP container. For more information on EL, visit java.sun.com/developer/EJTechTips/2004/tt0126.html |
Common Programming Error 27.5
Providing multiple page directives with one or more repeated attributes in common is a JSP translation-time error, unless the values for all repeated attributes are identical. Also, providing a page directive with an attribute or value that is not recognized is a JSP translation-time error. |
Software Engineering Observation 27.9
According to the JSP specification, section 1.10.1, the extends attribute "should not be used without careful consideration as it restricts the ability of the JSP container to provide specialized superclasses that may improve on the quality of rendered service." Remember that a Java class can extend exactly one other class. If your JSP specifies an explicit superclass, the JSP container cannot translate your JSP into a subclass of one of the container application's own enhanced servlet classes. |
Common Programming Error 27.6
Using JSP implicit object session in a JSP that does not have its page directive attribute session set to TRue is a translation-time error. |
27.7.2. include Directive
The include directive includes the content of another resource once, at JSP translation time. The include directive has only one attributefilethat specifies the URL of the resource to include. The difference between directive include and action is noticeable only if the included content changes. For example, if the definition of an XHTML document changes after it is included with directive include, future invocations of the JSP will show the original content of the XHTML document, not the new content. In contrast, action is processed in each request to the JSP. Therefore, changes to included content would be apparent in the next request to the JSP that uses action .
JSP includeDirective.jsp (Fig. 27.19) reimplements include.jsp (Fig. 27.7) using include directives. To test includeDirective.jsp in Tomcat, copy it into the jsp directory created in Section 27.3. Open your Web browser and enter the following URL
http://localhost:8080/jhtp6/jsp/includeDirective.jsp
Figure 27.19. JSP includeDirective.jsp demonstrates including content at translation time with directive include.
(This item is displayed on pages 1308 - 1309 in the print version)
"http://www.w3.org/1999/xhtml"> 8 9
1 "1.0"?> 2 "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 6 7 |
Using the include directive 10 23 24 | 25 26 27 32 36 37 38 42 46 47
|
Introduction to Computers, the Internet and the World Wide Web
Introduction to Java Applications
Introduction to Classes and Objects
Control Statements: Part I
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
GUI Components: Part 1
Graphics and Java 2D™
Exception Handling
Files and Streams
Recursion
Searching and Sorting
Data Structures
Generics
Collections
Introduction to Java Applets
Multimedia: Applets and Applications
GUI Components: Part 2
Multithreading
Networking
Accessing Databases with JDBC
Servlets
JavaServer Pages (JSP)
Formatted Output
Strings, Characters and Regular Expressions
Appendix A. Operator Precedence Chart
Appendix B. ASCII Character Set
Appendix C. Keywords and Reserved Words
Appendix D. Primitive Types
Appendix E. (On CD) Number Systems
Appendix F. (On CD) Unicode®
Appendix G. Using the Java API Documentation
Appendix H. (On CD) Creating Documentation with javadoc
Appendix I. (On CD) Bit Manipulation
Appendix J. (On CD) ATM Case Study Code
Appendix K. (On CD) Labeled break and continue Statements
Appendix L. (On CD) UML 2: Additional Diagram Types
Appendix M. (On CD) Design Patterns
Appendix N. Using the Debugger
Inside Back Cover