JSP Directives

0

What is JSP directives?

JSP directives provide directions and instructions to the container, telling it how to handle certain aspects of JSP processing.

A JSP directive affects the overall structure of the servlet class. It usually has the following form:

<%@ directive attribute="value" %>

Directives can have a number of attributes which you can list down as key-value pairs and separated by commas.

The blanks between the @ symbol and the directive name, and between the last attribute and the closing %>, are optional.
There are three types of directive tag:

Directive Description
<%@ page… %> Defines page-dependent attributes such as scripting language, error page, and buffering requirements.
<%@ include… %> Includes a file during the translation phase
<%@ taglib… %> Declares a tag library, containing custom actions, used in the page

 


 

The page Directive:

The page directive is used to provide instructions to the container that pertain to the current JSP page. You may code page directives anywhere in your JSP page. By convention, page directives are coded at the top of the JSP page.

Following is the basic syntax of page directive:

You can write XML equivalent of the above syntax as follows:

<jsp:directive.page attribute="value"/>

Attributes:

Following is the list of attributes with page directive:

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 wherther or not EL expression within the JSP page will be ignored
isScriptingEnabled Determines if scripting elements are allowed for use

 

Check more detail related to all the above attributes at Page Directive.

 The include Directive:

The include directive is used to includes a file during the translation phase. This directive tells the container to merge the content of other external files with the current JSP during the translation phase. You may code include directives anywhere in your JSP page.

The general usage form of this directive is as follows:

<%@ include file="relative url" >

Check more detail related to all the above attributes at Page Directive. The include Directive: The include directive is used to includes a file during the translation phase. This directive tells the container to merge the content of other external files with the current JSP during the translation phase. You may code include directives anywhere in your JSP page.

The general usage form of this directive is as follows:

<jsp:directive.include file="relative url"/>

The taglib Directive:

The JavaServer Pages API allows you to define custom JSP tags that look like HTML or XML tags and a tag library is a set of user-defined tags that implement custom behavior.

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.

The taglib directive follows the following syntax:

<%@ taglib uri="uri" prefix="prefixOfTag">

Where the uri attribute value resolves to a location the container understands and the prefix attribute informs a container what bits of markup are custom actions.

You can write XML equivalent of the above syntax as follows:

<jsp:directive.taglib uri="uri" prefix="prefixOfTag" />

JSP Actions

JSP actions use constructs in XML syntax to control the behavior of the servlet engine. You can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin.

There is only one syntax for the Action element, as it conforms to the XML standard:

<jsp:directive.taglib uri="uri" prefix="prefixOfTag" />

Action elements are basically predefined functions and there are following JSP actions available:

Syntax Purpose
Jsp:include Includes a file at the time the page is requested
Jsp:include Includes a file at the time the page is requested
Jsp:usebean Finds or instantiates a JavaBean
Jsp:setProperty Sets the property of a JavaBean
Jsp:getProperty Inserts the property of a JavaBean into the output
Jsp:forward Forwards the requester to a new page
Jsp:plugin Generates browser-specific code that makes an OBJECT or EMBED tag for the Jave plugin
Jsp:element Defines XML elements dynamically
Jsp:attribute Defines dynamically defined XML element’s attribute
Jsp:body Defines dynamically defined XML element’s body
Jsp:text Use to write template text in JSP pages and documents

 

Common Attributes: There are two attributes that are common to all Action elements:

  • id attribute and the scope attribute.  Id attribute: The id attribute uniquely identifies the Action element, and allows the action to be referenced inside the JSP page. If the Action creates an instance of an object the id value can be used to reference it through the implicit object PageContext
  • Scope attribute: This attribute identifies the lifecycle of the Action element. The id attribute and the scope attribute are directly related, as the scope attribute determines the lifespan of the object associated with the id. The scope attribute has four possible values: (a) page, (b)request, (c)session, and (d) application. 

The <jsp:include> Action

This action lets you insert files into the page being generated. The syntax looks like this:

<jsp:action_name attribute="value" />

Unlike the include directive, which inserts the file at the time the JSP page is translated into a servlet, this action inserts the file at the time the page is requested.

<jsp:include page="relative URL" flush="true" />

Following is the list of attributes associated with include action

Attribute Description        
Page The relative URL of the page to be included
Flush The Boolean attribute determines whether the included resource has its buffer flushed before it is included

Example:

Let us define following two files (a)date.jsp and (b) work.jsp as follows:

Following is the content of date.jsp file

<p>
    Today's day:<%= (new java.util.Date()).toLocaleString()%>
</p>

Here is the content of work.jsp file:

1

Now let us keep all these files in root directory and try to access work.jsp. This would display result something like this:

2

The <jsp:useBean> Action

The useBean action is quite versatile. It first searches for an existing object utilizing the id and scope variables. If an object is not found, it then tries to create the specified object.

The simplest way to load a bean is as follows:

<jsp:useBean id="name" class="package.class" />

Once a bean class is loaded, you can use jsp:setProperty and jsp:getProperty actions to modify and retrieve bean properties.

Following is the list of attributes associated with useBean action:

Attribute Description
Class Designates the full package name of the bean
Type Specifies the type of the variable that will refer to the object
beanName Gives the name of the bean as specified by the instantiate() method of the java.beans Beans class

 

Let us discuss about jsp:setProperty and jsp:getProperty actions before giving a valid example related to these actions

The <jsp:setProperty> Action

The setProperty action sets the properties of a Bean. The Bean must have been previously defined before this action. There are two basic ways to use the setProperty action:

You can use jsp:setProperty after, but outside of, a jsp:useBean element, as below:

<jsp:useBean id="myName"... />
...
<jsp:setPreperty name="myName" property="somePreoperty"... />

In this case, the jsp:setProperty is executed regardless of whether a new bean was instantiated or an existing bean was found.

A second context in which jsp:setProperty can appear is inside the body of a jsp:useBean element, as below:

<jsp:useBean id="myName"... >
...
 <jsp:setProperty name="myName" property="someProperty"... />
</jsp:useBean>

 

Here, the jsp:setProperty is executed only if a new object was instantiated, not if an existing one was found.

Following is the list of attributes associated with setProperty action:

Attribute Description
Name Designates the bean whose property will be set. The Bean must have been previously defined
Property Indicates the property you want to set. A value of ”*” means that all request parameters whose names match bean property names will be passed to the appropriate setter methods
Value The value that is to be assigned to the given property. The the parameter’s value is null, or the parameter does not exist, the setProperty action is ignored
Param The param attribute is the name of the request parameter whose value the property is to receive. You can’t use both value and param, but it is permissible to use neither

 

 

The <jsp:getProperty> Action

The getProperty action is used to retrieve the value of a given property and converts it to a string, and finally inserts it into the output.

The getProperty action has only two attributes, both of which are required ans simple syntax is as follows:

<jsp:useBean id="myName"... >
...
 <jsp:getPreperty name="myName" property="someProperty"... />
</jsp:useBean>

Following is the list of required attributes associated with setProperty action:

Attribute Description
Name The name of the Bean that has a property to be retrieved. The Bean must have been previously defined
Property The property attribute is the name of the Bean property to be retrieved

 

Example:

Let us define a test bean which we will use in our example:

Now use the following code in work.jsp file which loads the bean and sets/gets a simple String parameter:

3

 

Now try to access work.jsp, it would display following result:

4

Well we’ll conclude our discussion here in the next post we will learn some more interesting things related to jsp .

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge