Java2all.com  




Share This Topic -

2. JSP Directive elements:

A JSP directive gives special information about the page to JSP Engine.

Each JSP goes through two phases:

(1) The first phase is known as translation time, during which the JSP engine turns the file into a servlet. (The step following from 1 to 5 in JSP Architecture)

(2)The second phase is known as request time, during which the resulting servlet is run to actually generate the page. (The step following from 6 to 9 in JSP Architecture)

The JSP engine handles the directives at translation time because the JSP engine will translate a particular directive into a servlet only for the first time so the developer can get more speed for the process of loading a JSP file in a future.

Syntax:

<%@ directive-name [attribute=”value” attribute=”value”………….] %>

Note : The XML syntax for defining directives is

<jsp:directive.directiveType [attribute=”value” attribute=”value”………….]  />

Three main types of directives are:

a)     page directive
b)     include directive
c)     taglib directive

a) page directive :

The page directive is used to specify attributes for the JSP page as a whole.

The page directive does not produce any visible output when the page is requested but change the way the JSP Engine processes the page.

e.g., you can make session data unavailable to a page by setting a session to FALSE.

Syntax:

<%@ page [attribute=”value” attribute=”value”………….] %>

The table given below describes the possible attributes for the page directive.

Attribute

Description

Syntax

(default value is in bold)

Example

language

This tells the server about the language to be used in the JSP file. Presently the only valid value for this attribute is java.

language="java"

<%@ page language="java"%>

Import

This attribute defines the list of packages, each separated by comma .

import="package.class"

 

<%@ page import="java.util.*, java.io.*"%>

Extends

Indicates the superclass of servlet when jsp translated in servlet.

extends="package.class"

<%@ page extends="com.Connect"%>

Session

true indicates session should be bound to the existing session if one exists, otherwise a new session should be created and bound to it and false indicates that no sessions will be used.

session="true|false"

<%@ page session="true"%>

Buffer

specifies the buffer size for out and default is 8kb.

buffer="sizekb|none"

<%@ page buffer="8kb"%>

isThreadSafe

True indicates multithreading and false indicates that the servlet should implement SingleThreadModel

isThreadSafe="true|false"

<%@ page isThreadSafe="true"%>

autoFlush

true indicates that the buffer should be flushed when it is full and false indicates that an exception should be thrown when the buffer overflows.

autoflush="true|false"

<%@ page autoFlush="true"%>

Info

This defines a string that can be retrieved via the getServletInfo method.

info="message"

<%@ page info="This is a simple jsp file created by Java2all team on 22 Feb 2011"%>

pageEncoding

This defines data type of page encoding.

pageEncoding="ISO-8859-1"

<%@ page pageEncoding="ISO-8859-1"%>

contentType

This specifies the MIME type of the output.

contentType="MIME-Type"

<%@ page contentType="text/html; charset=ISO-8859-1"%>

isELIgnored

This defines ENUM data type.

isELIgnored=" true|false "

<%@ page isELIgnored="false"%>

isErrorPage

This indicates whether or not the current page can act as the error page.

isErrorPage="true|false"

<%@ page isErrorPage="false"%>

errorPage

Define a URL to another JSP that is invoked if an unchecked runtime exception is thrown.

errorPage="url"

<%@ page errorPage="error.jsp"%>

 <%@ page language="java" 
import="java.util.*,java.io.*" 
extends="com.Connect"
autoFlush="true"
errorPage="error.jsp"
info="This is a simple jsp file created by Java2all team on 22 Feb 2011"
isELIgnored="false"
isErrorPage="false"
session="true"
buffer="8kb"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
isThreadSafe="true"
%> 

NOTE : The XML equivalent of

<%@ page import="java.util.*" %>

is

<jsp:directive.page import="java.util.*" />

b) include directive :

It allows a JSP developer to include source code (static resource) of a file inside jsp file at the specified place at a translation time.

Typically include files are used for headers, footers, tables and navigation that are common to multiple pages.

The included page should not be another dynamic page so the included file does not need to be a complete and valid JSP or servlet.

Syntax:

<%@ include file="/folder_name/file_name"%>

If the included file and JSP file are available in the same folder then the folder name is not required in syntax.

<%@ include file="file_name"%>

Now we are describe the include directive by one simple example so the use of include directive in JSP file is easily understand.

Step-1 : Create two folder in webRoot  (i) HTMLFILE and (ii) JSPFILE.

Step-2 : Create a new HTML file(Included.html) in HTMLFILE folder.

Step-3 : Replace the source code of a file(Included.html) by the below code.

 <html>
  <head>
    <title> Included.html </title>
  </head>
  
  <body>
    <b>This is HTML page which is include in JSP file by using directive include tag in JSP file.</b> <br>
  </body>
</html> 

Step-4 : Create a jsp file(IncludeDemo.jsp) in JSPFILE folder.

Step-5 : Now replace the source code of a file(IncludeDemo.jsp) by the below code.

 <%@ page language="java" import="java.util.*,java.io.*" pageEncoding="ISO-8859-1"%>
<html>
  <head>
    
    <title> IncludeDemo.jsp </title>
  </head>
  
  <body>
    This is a JSP page. <br>
<%@ include file="/HTMLFILE/“Included.html"%>
  </body>
</html> 

Step-6 : Now Redeploy the Project and start the Server.

Step-7 : Enter the appropriate URL in web-browser.

Java2All.Com

 

Output :

This is a JSP page.
This is HTML page which is include in jsp file by using directive include tag in jsp file.

c) taglib directive :

The taglib directive makes custom actions available in current page through the use of tag library.

Syntax:

<%@ taglib uri="tag Library_path" prefix="tag_prefix"%>

uri  ====> The absolute path (URL) of a Tag Library Descriptor.

prefix ====> A unique prefix used to identify custom tags from library used later in the JSP page.

EX.

<%@ taglib uri="/tlds/TableGenerator.tld" prefix="tg"%>

And if TableGenerator.tld defines a tag named table, then the JSP file can contain tag of following type:

<tg:table>

……….

……….

</tg:table>

NOTE : We discuss taglib directory in detail in further chapter.