☰ See All Chapters |
Directive in JSP
Directive is an instruction to JSP container information on how the JSP container must translate a JSP page into a corresponding servlet. Using directive in JSP is optional. Directive has the following syntax:
<%@ directive attribute1="value1" attribute2="value2" ... %> |
There are three types of directives:
page directive
include directive
taglib directive
JSP page directive
Page Directive Syntax
<%@ page attribute1="value1" attribute2="value2" ... %> |
Attributes of page directive
Attribute | Value Type | Default Value |
autoFlush | Boolean | true |
buffer | Buffer size or false | 8192 |
contentType | MIME type, character set | text/html;charset=ISO-8859-1 |
errorPage | URL | None |
extends | Class name | None |
import | Fully qualified class name or package name | None |
info | String | Depends on server |
isErrorPage | Boolean | false |
isThreadSafe | Boolean | true |
language | Scripting language name | java |
session | Boolean | true |
autoFlush
autoFlush attribute accepts Boolean value and we can give true/false. Default value is true.
<%@ page autoFlush="false" %> |
true | JSP container will automatically flush the buffer when the buffer becomes full |
false | Programmer has to flush the buffer manually. To flush the buffer you can use flush method of the JspWriter which is as below: out.flush(); |
buffer
JSP container buffers the JSP page’s content to increase the performance.
The default size of the buffer is 8Kb.
The value will be considered in kilobytes. We can omit “Kb” or we can use "Kb" with number.
<%@ page buffer="16" %> |
<%@ page buffer="16" %> | Buffer of 16Kb will be used. |
<%@ page buffer="none" %> | No buffer will be used. |
contentType
The contentType attribute defines the Multipurpose Internet Mail Extension (MIME) type of the HTTP response sent to the client.
The default value is "text/html;charset=ISO-8859-1".
<%@ page contentType="image/gif exts=gif" %> |
errorPage
When an uncaught or unexpected exception comes from the application we have to redirect the user to error page instead of showing the user exception raised. Also we should not show any application related exception to users. The errorPage attribute specifies the URL of an error page that will be displayed if an uncaught exception occurs in this current JSP page.
<%@ page errorPage="errorPage.jsp" %> |
When the page is called, the error page is displayed. An error page must specify the isErrorPage attribute in its page directive and the value of this attribute must be "true".
<%@ page isErrorPage="true"%> |
extends
Developer should be extra cautious when using this attribute. The value of this attribute will be used by the generated servlet class of corresponding JSP. When we deploy the app in Tomcat server, the parent class that will be subclassed by the resulting servlet is HttpJspBase.
import
This is the very often used attribute.
The attribute is used to import a class or an interface or all members of a package.
Once JSP is compiled to servlet, the value of import attribute will be converted java import statements.
Example:
<%@ page import="java.io.*" %> <%@ page import="java.util.Set" %> |
info
info attribute is used to add information about page. Value of this attribute can be later retrieved by using the getServletInfo method.
<%@ page info="This page is used to create usr table" %> |
The default value for this attribute depends on the JSP container.
isErrorPage
When an uncaught or unexpected exception comes from the application we have to redirect the user to error page instead of showing the user exception raised. Also we should not show any application related exception to users. The errorPage attribute specifies the URL of an error page that will be displayed if an uncaught exception occurs in this current JSP page.
<%@ page errorPage="errorPage.jsp" %> |
When the page is called, the error page is displayed. An error page must specify the isErrorPage attribute in its page directive and the value of this attribute must be "true".
<%@ page isErrorPage="true"%> |
isThreadSafe
When JSP page is translated into servlets, generated servlets should be thread safe so that that simultaneous access to this page is safe. The default value of this attribute is true. Suppose if you want to serve only one request at a time by the JSP page (no simultaneous processing of multiple requests) set the value of this attribute to false.
<%@ page isThreadSafe="false" %> |
When the value of this attribute is false, the JSP container will serialize multiple requests for this page; that is, the JSP container will wait until the JSP servlet finishes responding to a request before passing another request to it.
language
The value of this attribute should be the language used in coding the JSP. Most of time this will be “java” only. Also default value is “java”. All JSP containers must support Java as the scripting language. Tomcat supports only “java”, there are some jsp container which supports other languages like XML and XSLT. In those cases where JSP is having the other language we can specify that using language attribute.
<%@ page language="java" %> |
session
By default all the JSP pages participate in session and session management. If the value of the session attribute is set to false then JSP will not be maintained in session. Unless necessary we should not allow JSP pages to add in session, this consumes unnecessary memory in server. If page is not required in session we have to set the session attribute to false.
<%@ page session="false" %> |
JSP include directive
Using this directive you can include the contents of other files in the current JSP page.
include directive is useful if you have a contents which remains static throughout the application. In most of the applications header and footer remains same in all the pages, in such cases instead of duplicating the header, footer code in all JSP pages, we can just include header and footer in JSP pages if required.
You can include HTML, JSP, and XML etc... If you are including a JSP page, the included JSP page itself can include another file.
The syntax for the include directive is as follows:
<%@ include file="resourceRelativeURL" %> |
If the resourceRelativeURL begins with a forward slash character (/), it is considered as an absolute path of the server. If resourceRelativeURL does not start with a "/", it is considered as relative to the current JSP page.
Using include directive we cannot include external resource, resource should be inside server.
JSP include directive example
Project Directory Structure
header.html
<!DOCTYPE html> <html> <body> <h1>I am Header</h1> </body> </html> |
footer.html
<!DOCTYPE html> <html> <body> <h1>I am Footer</h1> </body> </html> |
home.jsp
<html> <head> <title>include directive</title> </head> <body> <%@ include file="/includes/header.html"%> <% out.println("Main Contents"); %> <%@ include file="/includes/footer.html"%> </body> </html> |
Output
JSP taglib directive
In JSP we can define custom tags, say for example, <hello>. When we use this it displays “Hello World!”. Also there are many vendors they provide certain custom readymade tags.
Custom tags definition will be given in a file called Tag Library Descriptor with .tld extension. (We will study more about custom tags in later chapters.)
The JSP taglib directive is used to refer a tag library that defines tags.
<%@ taglib uri="tagLibraryURL" prefix="tagLibraryPrefix" %> |
All Chapters