MY mENU


Monday 21 May 2012

JSP Directives


JSP Directives

JSP directives instruct the JSP container on how to work with JSPs. There are three directives in the JSP specification: page, include and taglib. The directive form usualy follows the following form:
<%@ directive attribute1 = "value 1" 
              attribute2 = "value 2"
              ...
              %>

page Directive

The page directive allows you to define one or more following attributes:
import Option
import="package.class"
import="package.class1,...,package.classN"
Import option allows you to specify which classes or packages are used in the page. for example, you can import all the classes in the package java.util as follows: 
<%@ page import="java.util.*" %>
You can use  import option multiple times in the page directive.
contentType option
contentType="MIME-Type"
 contentType="MIME-Type; charset=Character-Set".
The contentType option allows you specify the MIME type of the output page. The default istext/html you can also use other values such as text/plain for outputing text format  such as
<%@ page contentType="text/plain" %>
IsThreadSafe Option
isThreadSafe="true|false". 
isThreadSafe option allows you to indicate the page is being processed as thread-safe. The default value of the isThreadSafe is true therefore all JSPs are considered thread-safe. IfisThreadSafe is set to false, JSP engine ensures that only one thread at a time is executing the current JSP page 
.session Option
session="true|false". 
By using session option, you are telling JSP compiler that you want to use session or not. The default value of the session attribute is true, indicating that there is an implicit variable called session is available for your use.

buffer and autoFlush Options

buffer and autoFlush options let you control the JSP buffering. 
You can turn buffer on or off or even specify the buffer size by using buffer option such as:
<%@ page buffer="none" %>
<%@ page buffer="64kb" %>
 
autoFlush option controls whether the buffer is flushed automatically when it is full.  The default value of autoFlush is true means JSP automatically flush the buffer when it is full.

Info Option

Info option allows you to define the description of the servlet. Later on you can access this value by calling the method:
Servlet.getServletInfo(). 

extends Option

In some cases, you need to create your own superclass of JSP pages and using this supper class in different JSPs. In order to do so, first you need to create a super class first. This supper class normally inherits from the HttpServlet class. And in the JSPs you use theextends option to use that supper class in you JSP page.

isELIgnored Option

isELIgnored option provide the ability for you to disable the evaluation of the expression language (EL). You will learn more about expression language in the later tutorial.

errorPage Option

errorPage option allows you to indicate the error page to be displayed when the error occurs in the current executing page. For example:
<%@ page errorPage="error.jsp" %>

isErrorPage Option

isErrorPage option indicates that the current JSP page can be used as an error page for other JSP page. When you set isErrorPage equal true, JSP engine creates an implicit exception object which contains the Throwable object that caused the error page to be called. By using this excpetion object, you can print out the error message to the user, for example:

Include Directive: Include directive lets you to include a file (JSP or HTML) at the time JSP engine translate the JSP page into a servlet.  The syntax of include directive is as follows:
<%@ include file="relative url" %>
The filename you specify in the include directive is a relative URL. If you provide a file name without associated path, the JSP compiler always assumes that the file is in the same directory as the JSP page.
Normally the include directive is used to include common sections of  a JSP page in a webapplication or website such as header, navigation bar, footer... to make those section reusable in every JSP page.

taglib Directive

JSP has a set of standard tags for you to use. JSP also provide a possibility to allow you to create new custom tags look like HTML and XML tag. In this case, you can use taglib directive to use your custom tag in your JSP page. For using custom tag, you can refer to the custom tag tutorial. The syntax of using taglib directive is as follows:
<%@ taglib uri="http://localhost/jsptutorial/taglib/mytaglib"
   prefix="jsptutorial" %>