x

JSP Page Directive

PREV     NEXT

JSP directives do not display anything. However directives are used to change the functionality of a JSP page. They are used to pass a message to JSP container. Directives begin with <%@ and end with %>. There are three JSP directives –


  1. page directive
  2. include directive
  3. taglib directive

The page directive can take many different attributes. One page directive element can have multiple attributes. The list of these attributes is included below –

  1. info attribute
  2. language attribute
  3. contentType attribute
  4. pageEncoding attribute
  5. extends attribute
  6. import attribute
  7. session attribute
  8. buffer attribute
  9. autoflush attribute
  10. errorPage attribute
  11. isThreadSage attribute
  12. isELIgnored attribute

Info attribute of page directive:

The info attribute of page directive is used to provide an informational message about the JSP page. The JSP container ignores the info attribute. The info attribute is meant for JSP programmers to help them understand about the functionality of the page.

<%@page info=”JSP tutorial by Javaprepare.”%>

The equivalent code in XML-compatible syntax is –

<jsp:directive.page info=”JSP tutorial by Javaprepare.”/>

Language attribute of page directive:

JSP containers may support languages other than Java. However most popular JSP container support only Java. The language attribute of page directive is used to specify the scripting language. The syntax for the language attribute is illustrated below –

<%@page language=”java”%>

The language attribute is set to java by default. So most JSP programs do not specify the language attribute of page directive.

contentType attribute of page directive:

The contentType attribute of page directive is used to specify the format of the document that JSP will generate. Example below specifies that the generated file is of HTML format.

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

The three commonly used values of contentType are –

contentType Generated file type
text/HTML HTML file
text/xml XML file
text/plain text file

pageEncoding attribute of page directive:

The pageEncoding attribute of page directive is used to specify the character set of the generated file. The default character set is ISO-8859-1. Example below illustrates the use of pageEncoding attribute of page directive.

<%@page pageEncoding=”ISO-8859-1″%>

extends attribute of page directive:

JSP pages are converted into servlets behind the scenes by JSP container. The extends attribute of page directive lets you specify the base class for the servlet that JSP container generates for the JSP page. The example below illustrates the use of extends attribute of page directive.

<%@page extends=”myServletClass”%>

extends attribute usage is very rare. JSP containers default behavior almost always suffices.

import attribute of page directive:

The import attribute of page directive has the same purpose as the Java import statement. If the page uses a Java class, then its complete path (including the package hierarchy) can be specified with the import directive.

<%@page import=”java.lang.Math”%>
square root of 25 is <%Math.sqrt(25); %>

In the above example JSP container knows the path of the Math class because of the import statement. Hence when the sqrt method is invoked, complete class name of the Math class need not be specified. import attribute is the only attribute of page directive which can take multiple values. For all other attributes of the page directive, at most one value can be specified in the JSP file. So the following is legal JSP.


<%@page import=”java.util.List”%>
<%@page import=”java.util.ArrayList”%>

All classes of the following four packages are imported by default into JSP pages:

  1. java.lang
  2. javax.servlet
  3. javax.servlet.http
  4. javax.servlet.jsp

No import statement is required in JSP files for classes in the above packages.

session attribute of page directive:

JSP container provides in-built session management. If the JSP page does not need session management, then the session attribute can be set to false. The syntax of the session attribute of page directive is included below:

<%@page session=”false”%>

By default, session attribute is set to true, i.e. Session management is enabled by default. If session attribute is set to false, the session implicit variable is not available to the JSP page.

buffer attribute of page directive:

The buffer attribute of page directive is used to specify behavior of JSP container with respect to sending the output to an intermediate buffer, before it is sent to Http response. The default size of buffer is 8KB. This means that the JSP container will send the HTTP response in chunks of 8 KBytes.

The feature can be disabled by setting the buffer attribute to none. In this case, JSP container does not perform any buffering, and sends contents to the client as they are generated.

<%@page buffer=”none”%>

The buffer attribute of page directive may also be used to change the buffer size. The example below sets the buffer size to 4 KBytes.

<%@page buffer=”4kb”%>

autoflush attribute of page directive:

The autoflush attribute of page directive is used to specify JSP containers behavior when the buffer gets full. autoflush attribute takes boolean values true or false as input. The default value for autoflush is true. The default behavior is that after the buffer is full, the page’s output is sent as response. If autoflush is set to false, JSP container will raise an exception if the buffer gets full. If autoflush is false, then an error page gets generated, when the buffer gets filled.

<%@page autoflush=”false”%>

If buffer is set to none, autoflush cannot be set to false.

errorPage attribute of page directive:

The errorPage attribute of page directive defines the behavior of the application if the JSP container gets any error when processing a page. The errorPage attributes is set to the URL of a the page. If the JSP page generates an exception, control is transferred to the URL specified by errorPage. The example below illustrates the usage of autoflush attribute of page directive.

<%@page errorPage=”./error.jsp”%>

isErrorPage attribute of page directive:

The isErrorPage attribute of page directive is used to specify that the page may be used as an error page. It takes boolean value true or false. The default value of isErrorPage is false. The example below sets the error page to error.jsp for current page.

<%@page isErrorPage=”true”%>

The exception implicit object is available to a JSP page, only if isErrorPage is set to true.

isThreadSafe attribute of page directive:

The isThreadSafe attribute of page directive informs the JSP container how the JSP page should behave if multiple requests are received at the same time. It takes boolean values true or false. If isThreadSafe attribute is set to true, then multiple request received for the JSP page are handled simultaneously. The default value of isThreadSafe attribute is true. If the isThreadSafe attribute is set to false, then requests to the JSP page are processed sequentially. The example below illustrates the usage of isThreadSafe attribute of page directive.

<%@page isThreadSafe=”true”%>

isELIgnored attribute of page directive:

The isELIgnored attribute takes boolean value of true or false as input. If isELIgnored is true, then any Expression Language in JSP is ignored. The default value of isELIgnored is false. The example below illustrates the usage of isELIgnored attribute of page directive.

<%@page isELIgnored=”true”%>

PREV     NEXT



Like it? Please Spread the word!