JSP Page Directive
The JSP directives are special JSP messages that tells the web container how to translate a JSP page into the servlet.
There are three types of directives - page directive,include directive, taglib directive
<%@ directive attribute="value" %>
A. JSP Page Directive :
- The Page directive used to define a set of attributes that apply to an complete JSP page
- Page directives can be placed anywhere in your JSP page.
- Generally, Page directives are written at the top of the JSP page.
Syntax of Page Directive :
<%@ page attribute="value" %>
XML equivalent of the above syntax as follows :
<jsp:directive.page attribute="value" />
B. List of Page Directive Attributes :
Attribute | Purpose |
---|---|
buffer | Specifies a buffering model for the output stream. |
autoFlush | Controls the behavior of the servlet output buffer. |
contentType | Defines the character encoding scheme. |
errorPage | Defines the URL of another JSP that reports on Java unchecked runtime exceptions. |
isErrorPage | Indicates if this JSP page is a URL specified by another JSP page's errorPage attribute. |
extends | Specifies a superclass that the generated servlet must extend |
import | Specifies a list of packages or classes for use in the JSP as the Java import statement does for Java classes. |
info | Defines a string that can be accessed with the servlet's getServletInfo() method. |
isThreadSafe | Defines the threading model for the generated servlet. |
language | Defines the programming language used in the JSP page. |
session | Specifies whether or not the JSP page participates in HTTP sessions |
isELIgnored | Specifies whether or not EL expression within the JSP page will be ignored. |
isScriptingEnabled | Determines if scripting elements are allowed for use. |
C. Examples of Page Directives
1. Import Page Directive
- The import page directive attribute is used to import java packages.
- The value of import option is the name of the package you want to import.
In order to import java.sql.*
use the following page directive:
<%@ page import="java.sql.*" %>
To import multiple packages comma is used as separator -
<%@ page import="java.sql.*,java.util.*" %>
Following Packages are imported by default in the JSP -
java.lang.* |
---|
javax.servlet.* |
javax.servlet.jsp.* |
javax.servlet.http.*. |
Example of the import Directive :
<html> <body> <%@ page import="java.util.Date" %> Today is : <%= new Date() %> </body> </html>
Output :
Today is: Sun Apr 13 12:03:01 IST 2014
2. Buffer Page Directive Attribute :
- The buffer attribute sets the buffer size in kilobytes in order to handle output generated by the JSP page.
- The buffer attribute is used to provide buffering ability to the server’s output response object.
- Servlet will be immediately directed to the response object if we specify the buffer attribute value to “none”
- Below statement is used to direct the servlet to write output to a buffer of size not less than 8 kilobytes
- The default size of the buffer is 8Kb
<%@ page buffer="none" %>
Buffer Parameter | Explanation |
---|---|
none | To direct the servlet to write output directly to the response output object |
8kb | To direct the servlet to write output to a buffer of size not less than 8 kilobytes |
16kb | To direct the servlet to write output to a buffer of size not less than 16 kilobytes |
3. autoFlush Attribute:
autoFlash Value | Meaning |
---|---|
autoFlush='false' | Causes the servlet to throw an exception when the servlet's output buffer is full |
autoFlush='true' | Buffer will be flushed automatically when servlet's output buffer is full |
The buffer and autoFlush attributes are written on a single page directive as follows -
<%@ page buffer="16kb" autoflush="true" %>
4. The errorPage Attribute:
- The errorPage attribute is used to tell JSP engine which page to display if there is an error while processing JSP.
- The value of the errorPage attribute is a relative URL.
- The errorPage attribute is used to display alternate page when JSP throws uncaught exceptions
<%@ page errorPage="error.jsp" %>
5. isErrorPage Attribute:
- isErrorPage attribute is used to indicate that the current JSP is used as the error page for another JSP.
- Value of isErrorPage can be either true or false.
- Default value of the isErrorPage attribute is false.
For example, the handleErrorMsg.jsp
page sets the isErrorPage attribute to true because it is used to handle errors:
<%@ page isErrorPage="true" %>
6. extends Attribute:
- generated servlet will extend the class specified in the extends attribute.
- extends attribute specifies a superclass which must be extended by specified class.
For example, the following directive directs the JSP translator to generate the servlet such that the servlet extends somePackage.SomeClass:
<%@ page extends="somePackage.SomeClass" %>
7. info Attribute:
The info attribute lets you provide a description of the JSP. following is a coding example:
<%@ page info="This JSP Page Written By ZARA" %>
8. isThreadSafe Attribute:
- isThreadSafe option marks a page as being thread-safe.
- By default, all JSPs are considered thread-safe.
If you set the isThreadSafe option to false, the JSP engine makes sure that only one thread at a time is executing your JSP. The following page directive sets the isThreadSafe option to false:
<%@ page isThreadSafe="false" %>
9. language Attribute:
language attribute indicates the programming language used in scripting the JSP page.
<%@ page language="java" %>
10. session Attribute:
- Session attribute used to indicate whether JSP page uses HTTP sessions or not.
- If session attribute value is True then JSP page has access to a built-in session object
- If session attribute value is False then JSP page cannot access the built-in session object.
- Methods used for accessing session - session.getCreationTime() or session.getLastAccessTime()
<%@ page session="true" %>
11. isELIgnored Attribute:
- isELIgnored attribute is used to disable the evaluation of Expression Language (EL) expressions.
- Default value of the attribute is true, meaning that expressions, ${…}, are evaluated as dictated by the JSP specification.
- If the attribute is set to false, then expressions are not evaluated.
<%@ page isELIgnored="false" %>
12. isScriptingEnabled Attribute:
- isScriptingEnabled attribute used to determine whether scripting elements are allowed or not.
- the default value (true) enables scriptlets, expressions, and declarations.
- If the attribute’s value is set to false, a translation-time error will be raised if the JSP uses any scriptlets, expressions (non-EL), or declarations.
- You can set this value to false if you want to restrict usage of scriptlets, expressions (non-EL), or declarations:
<%@ page isScriptingEnabled="false" %>
13. The contentType Attribute:
It tells us what will be the content of the generated page.
<%@ page contentType="text/xml" %>
above line will tell that “Hey browser, Please render the page generated by JSP as XML document”.
contentType Value | Meaning |
---|---|
text/xml | Directs the browser to render the generated page as XML |
text/html | Directs the browser to render the generated page as HTML |
application/msword | Directs the browser to render the generated page as Microsoft Word document |
text/html:charset=ISO-8859-1 | Directs the browser to render the generated page as HTML with ISO Latin 1 encoding |