Directives
JspDirectives are used to give instructions to container for translation of jsp to Servlet code.
There are three directives in jsp.
- Page directive
- Include directive
- taglib directive
1. Page directive
The Page directive defines a number of page dependent properties which communicates with the Web Container at the time of translation
Following are the different attributes of page directive.
<%page language="java" contentType="any MIME type" extends="some java class" import="package.*,package.class" session="true/false" isThreadSafe=""true/false" errorPage="some JSP" isErrorPage="true/fasle" autoFlush="true/flase" buffer="any amount of kb" isELIgnored="true/false"%>
language
Defines the programming language used in JSP page.
<%@ page language="java" %>
contentType
This attribute is used to set the content type and character set of the response. The default value of contentType attribute is”text/html; charset=ISO-8859-1″. We can use it like below.
<%@ page contentType="text/html; charset=US-ASCII" %>
extends
Specifies a superclass that the generated servlet must extend.this is very rarely used
<%@ page extends="org.apache.jasper.runtime.HttpJspBase" %>
import attribute
It’s used to instruct container to import other java classes.It is similar to the import statement in java programming language.
%@ page import="java.sql.*" %><%@ page import="java.sql.*,java.util.*" %>
session
Specifies whether client joins a HTTP session. It’s default value is true and session is created. To disable the session creation, we can use it like below.
<%@ page session ="false" %>
isThreadSafe
We can use this attribute to implement SingleThreadModel interface in generated servlet. If the value is “true” for this attribute the generated class will follow multithreaded modelwhich is default.If the value is “false ” it will follow single threaded model.
<%@ page isThreadSafe="false" %>
errorPage
Defines the URL to another JSP ,ifamy exception occurs in this JSP and is used for exception handling.
<%@ page errorPage="errorHandler.jsp" %>
isErrorPage
Defines whether current JSP is an error page or not. JSP implicit attribute exception is available only to the error page JSPs.
<%@ page isErrorPage="true" %>
autoFlush
autoFlush attribute is to control the buffer output. Its default value is true and output is flushed automatically when buffer is full. If we set it to false, the andbuffer is full, we will get exception for buffer overflow.
<%@ page autoFlush="true" %>
buffer
we can use this attribute to set the buffer size in KB to handle output generated by JSP page. Default value of buffer attribute is 8kb. We can define 16 KB buffer size as;
<%@ page buffer="16kb" %>
isELIgnored
We can ignore the Expression Language (EL) in JSP using this page directive attribute. Its default value is false, so EL is enabled by default.
<%@ page isELIgnored="true" %>
2.include directive
The include directive is used to includes a file during the translation phase.The purpose of include is to merge the contents of one JSP source file into another.
<%@ include file="filename.jsp" %>
download the example.
3.taglib directive
This directive allows user to develop and use Custom tags in JSP.
The taglib directive declares that your JSP page uses a set of custom tags, identifies the location of the library, and provides a means for identifying the custom tags in your JSP page.
<%@tagliburi="taglibURI" prefix="tag prefix"%>
For creating any custom tag, we need to follow following steps:
- Create the Tag handler class and perform action at the start or at the end of the tag.
- Create the Tag Library Descriptor (TLD) file and define tags
- Create the JSP file that uses the Custom tag defined in the TLD file.
download the example
simple and good explanation for beginners.