Directives

 < Free Open Study > 



Directives are messages sent by the JSP author to the JSP container to aid the container in the process of page translation. These directives are used by the JSP container to import tag libraries, import required classes, set output buffering options, and include content from external files. The JSP specification defines three directives:

  • page - provides general information about the page, such as the scripting language that is used, content type, or buffer size

  • ?include - used to include the contents of external files

  • ?taglib - used to import custom actions defined in tag libraries

The page Directive

The page directive sets a number of page dependent properties that can be used during the translation phase by the container. A JSP page can contain any number of page directives. However only the import attribute should appear more than once.

The following table shows the different attributes associated with the page directive:

Attribute

Description

language

Defines the server-side scripting language used in the JSP. The only language that is currently supported is Java.

extends

Defines the name of the class the page implementation class should extend. This attribute is generally not used by page authors. It is left to the container to decide the ancestor of the page implementation class.

import

Allows us to import packages and classes to be used within scriptlets. The packages and classes are defined as a comma separated list.

session

Defines whether the page should participate in a session. The session implicit variable is available for scripting only if the value of this attribute is set to true. The default value is true.

buffer

Defines the buffer size for the JSP in kilobytes. If set to none, the output of the JSP is not buffered.

autoFlush

If set to true the buffer will be flushed when the maximum limit is reached. Otherwise an IllegalStateException is thrown.

isThreadSafe

If set to false, the page implementation class will implement the SingleThreadModel interface. The default value is true.

info

The value of this attribute is returned by the getServletInfo() method of the page implementation class.

errorPage

Defines the relative URI of the web resource to which the response should be forwarded if an exception is thrown in the JSP page.

contentType

Defines the MIME type for the output response. The default value is text/html.

isErrorPage

Should be set to true for JSP pages that are defined as error pages.

pageEncoding

Defines the character encoding for the page.

The following snippet shows an example of the page directive:

     <%@ page language="java" buffer="10Kb" autoFlush="true"              errorPage="/error.jsp" import="java.util.*,javax.sql.RowSet" %> 

This directive defines the following properties for the JSP page:

  • The scripting language is set as Java

  • The buffer size is set to 10 kilo bytes

  • The container will flush the contents if it exceeds the specified size

  • The error page is defined as error.jsp

  • The JSP page may use classes and interfaces from the java.util package and the interface javax.sql.RowSet within its scriptlets.

The include Directive

The include directive is used to insert template text and JSP code during the translation phase. The contents of the included file specified by the directive is included in the including JSP page. For example:

     <%@ include file="included.jsp" %> 

The included file is searched relative to the location of the current JSP unless it is preceded with a forward slash.



 < Free Open Study > 



Professional Java Servlets 2.3
Professional Java Servlets 2.3
ISBN: 186100561X
EAN: 2147483647
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net