JSP Page Directive

Page Directives in JSPs are instruction from JSPs to JSP Engine. The page directive is used to provide instructions to the container that pertain to the current JSP page. You may code page directives anywhere in your JSP page. By convention, page directives are coded at the top of the JSP page.

General syntex of JSP Page Directives generally look like this:

<%@ page attribute1="value1" attribute2="value2" %>

Syntex:

<%@ page import="xyz" language="xyz" extends="xyz" session="xyz" buffer="xyz" isThreadSafe="xyz" autoFlush="xyz" errorPage="xyz" info="xyz" contentType="xyz" isErrorPage="xyz" %>

Example:

<%@ page import="java.util.*" %>

Explanation: As shown above, Page directives has many attributes. Page Directive can be used multiple times in a single JSP. However, except import attribute, no other attirbute can be used multiple times.

language Attribute: At present JSP supports only JAVA as a language attirbute.

<%@ page language="java" %>

import Attribute: import Attribute in Page Directive allows to import JAVA packages to JSP. You can import more than one package as comma seperated values or you can write multiple Page Directive each having its own import attribute.

<%@ page import="java.util.*" %>

multiple import

<%@ page import="java.util.*,java.sql.*" %>

extends Attribute: extends Attribute allows to extend Super Class in JSP.

<%@ page extends="com.dineshonjava.jsp.SomeClass"  %>

session Attribute: session Attribute can have either true or false value. Default value is always true. If it is set to false, session object can not be used in JSP.
he session attribute indicates whether or not the JSP page uses HTTP sessions. A value of true means that the JSP page has access to a builtin session object and a value of false means that the JSP page cannot access the builtin session object.

Following directive allows the JSP page to use any of the builtin object session methods such as session.getCreationTime() or session.getLastAccessTime():

<%@ page session="true" %>

buffer Attribute: You can specify either none or some value in kb. Default value is 8kb. This value is used by implicit object “out” to buffer output from compiled JSP to client’s web browser.

<%@ page buffer="none" %>

autoFlush Attribute: autoFlush Attribute in JSP has either true or false value. Default value is set to True. That means when buffer overflows, it automatically flush the buffer. If it is set to false, JSP will throw an error when buffer overflows.

<%@ page autoFlush="false" %>

This directive causes the servlet to flush the output buffer when full:

<%@ page autoFlush="true"%>

Usually, the buffer and autoFlush attributes are coded on a single page directive as follows:

<%@ page buffer="16kb" autoflush="true"%>

isErrorPage Attribute: isErrorPage Attribute in Page Directive has either true or false value. Default value is set to false. Implicit object exception can not be used if isErrorPage value is false. In order to use exception object in JSP, isErrorPage attribute value must be set to true.

<%@ page isErrorPage="true"%>

errorPage Attribute: errorPage Attribute in Page Directive points to the page which handles exception thrown in current jsp. Location of the errorPage jsp is shown using relative path.

<%@ page errorPage="customErrorPage.jsp"%>

contentType Attribute: contentType Attribute in Page Directive suggest the MIME Type and Character Encoding for JSP. Default value for MIME Type is text/html and default value for Character Encoding is ISO-8859-1.

<%@ page contentType="text/html"%>

The following directive sets the content type as a Microsoft Word document:

<%@ page contentType="application/msword"%>

isThreadSafe Attribute: isThreadSafe Attribute in Page Directive has either true or false value. Default value is set to true. That means, JSP engine sends mutiple requests to JSP same time. Code inside JSP must be synchronized to handle concurrent reqests. If the value of the attribute is set to false, JSP Engine will send only one reqest at a time to JSP.

<%@ page isThreadSafe="false"%>

info Attribute: info Attribute in Page Directive is just a text String.

<%@ page info="This is JSP Page"%>

<<Previous <<   || Index ||   >>Next >>
Previous
Next