The JSP page directive


Page directive is used to specify a number of page specific attributes such as content type, page encoding, classes to import and buffering model etc.  A translation unit can contain more than one instance of page directive, in any order, anywhere and all attributes will be applied together to the whole translation unit. However there are few exceptions.

  1. pageEncoding and contentType attributes must appear at the beginning of the page.
  2. An attribute must appear only once in a translation unit unless the values of the attribute are same for all occurrences, otherwise a translation error will occur. The import and pageEncoding can appear more than once in a translation unit.
  3. pageEncoding attribute can appear at most once per a file otherwise a translation error will occur.

Note:  A JSP source file and any other files included with include directive is called the translation unit.

Below is a complete list of attributes that can be used with page directive.

 Attribute name
 Description  Default value
 language  Specifies the scripting language used in scriptlets, expressions and declarations in the JSP page  java
 extends  The fully qualified name of a java class, that defines the super class of the JSP page’s implementation servlet  None
 import  The comma separated list of class names that will be imported in resulting servlet. It is similar to java import statement.  java.lang.*, javax.servlet.*,
javax.servlet.jsp.* and javax.servlet.http.*

 session  Indicates that the page participates in session. If the value is true, an implicit variable named session of type javax.servlet.HttpSession is available and references current session. If the value is false than implicit variable session is unavailable and any attempt to use it within the body of the page will result in translation error.  true
 buffer  Specifies the buffering model for the initial JspWriter to handle the response generated by the page. If the value specified is none Than there is no buffering and all out put is written immediately through PrintWriter. The size of the buffer can be specified in kilobytes, kb suffix is required otherwise translation error will occur. If a buffer size is specified then output is buffered with a
buffer size not less than that specified. The default value is not less than 8kb
 Not less than 8kb
 autoFlush  Indicates weather the buffered output should be flushed automatically when the buffer is full, or weather an exception should be raised indicating buffer overflow. autoFlush can not be set to true when buffer is none, otherwise it will result in translation error.  true
 isThreadSafe  Indicates the threading model to be used by JSP and generated servlet class.
True means the JSP is thread safe and container can send multiple simultaneous requests to the page. Value of false means the JSP page is not thread safe and container must send a request at a time, in other words container must serialize the requests to JSP page
Note: The Servlet 2.4 specification deprecates
SingleThreadModel, which is the most common
mechanism for JSP containers to implement isThreadSafe.
Page authors are advised against using isThreadSafe, as the generated Servlet may contain deprecated code.
 true
 Info  Used to provide the information about the JSP page, any arbitrary string can be specified that can subsequently be obtained from the
page’s implementation of Servlet.getServletInfo method.
 None
 isErrorPage  Indicates weather or not the page is intended to be an error page of some other JSP page. If value is true than an implicit scripting language variable exception is available that references the exception. If the value is false than implicit object ex caption is unavailable and any attempt to use it within the body of the JSP page will result in translation time error.  false
 errorPage  Defines the URL to another resource withing the currenct web application which is invoked if any checked or unchecked exception is thrown but not caught by the JSP page’s implementation class. If the URL points to another JSP page than, when that JSP page is invoked an implicit object exception is available which references the originating uncaught Throwable.
When an uncaught exception occurs, JSP page’s implementation servlet class catches the instance of Throwable and passes it to error page by setting it in ServletRequest using setAttribute() method, with the name of javax.servlet.jsp.jspException and javax.servlet.error.exception.
The path of a servlet can also be specified as the value of errorPage attribute, In that case the original Throwable can be obtained by getAttribute() method of the request with any name specified above.
 None
 contentType  Defines the MIME type for the response of the JSP page and character encoding for the JSP page and response. This can have either of the form”MIMETYPE” or “MIMETYPE; charset=CHARSET”.
See http://www.iana.org/assignments/media-types/index.html for more information on MIME types. Default value for MIMETYPE of the generated response is text/html for jsp page in standard syntax and text/xml for JSP documents in XML syntax.
 The default value for CHARSET is ISO-8859-1.
 pageEncoding  Defines the character encoding for the JSP page.
If pageEncoding attribute is not defined and CHARSET is defined in contentType attribute than it will be used as character encoding of the page.
 default value is ISO-8859-1
 isELIgnored  Defines whether EL expressions are ignored or evaluated for
this page and translation unit. If true than EL expressions are ignored by JSP engine, if false than EL expression are evaluated when they appear in template text or action attributes.
 false


An example of JSP page directive

Following page directive imports List and ArryList classes so it can be used within page.

<%@page import=”java.uitl.list, java.util.ArrayList”/>

Following page directive specifies that the page is not thread safe so container serializes the requests to JSP page and will not send multiple simultaneous requests.

<%@page isThreadSafe=”false”/>

In JSP documents in XML format, page directive can be written as

<jsp:directive.page language="”java”," import="”java.util.List”/">
</jsp:directive.page>

Following is a complete example of a page directive.

<%@page language=”java” import=”java.util.List, java.util.Date” Session=”true” buffer=”24kb” autoFlush=”true” Info=”JSP page directive tutorial” errorPage=”error.jsp” isErrorPage=”false” isThreadSafe=”true” contentType=”text/html” pageEncoding=”ISO-8859-1” />

For more details on JSP page directive, see http://java.sun.com/products/jsp/tags/11/syntaxref11.fm7.html

DESCRIPTION OF ATTRIBUTES ARE NOT VISIBLE

DESCRIPTION OF ATTRIBUTES ARE NOT VISIBLE

THE TABLE TAG IN THIS PAGE IS NOT VIEWED PROPERLY.