JSP Tags

JSP scripting language include several tags or scripting elements that performs various tasks such as declaring variables and methods, writing expressions, and calling other JSP pages. These are known as JSP scripting elements. The different types of scripting elements are summarized in the Table 1

Table 1. JSP Tags

JSP Tag

Brief Description

Tag Syntax

Directive

Specifies translation time instructions to the JSP engine.

<%@ directives %>

Declaration

Declaration Declares and defines methods and variables.

<%! variable dceclaration & method definition %>

Scriptlet

Allows the developer to write free-form Java code in a JSP page.

<% some Java code %>

Expression

Used as a shortcut to print values in the output HTML of a JSP page.

<%= an Expression %>

Action

Provides request-time instructions to the JSP engine.

<jsp:actionName />

Comment

Used for documentation and for commenting out parts of JSP code.

<%– any Text –%>

 

Example code showing different types of JSP Tags:

<%-- Counter.jsp --%>             <%-- Comment Tag --%>

<%@ page language="java" %>       <%-- Directive Tag --%>
<%! int count = 0; %>             <%-- Declaration Tag --%>
<% count++; %>                    <%-- Scriptlet Tag --%>
Welcome! You are visitor number
<%= count %>                      <%-- Expression Tag --%>

Note: Here, by using comment tag, example of five JSP tags are shown.

 

Discussions about the JSP Tags

Different types of JSP Tags are discussed below one-by-one.

 

1. Directive Tag:

Directive tags provide general information about the JSP page to the JSP engine. A directive tag always starts with <%@ and ends with %>.

There are 3 types of directives: page, include, and taglib.

The general syntax for the 3 directives is:

<%@ page attribute-list %>
<%@ include attribute-list %>
<%@ taglib attribute-list %>

In the above shown syntax, the attribute-list represents one or more attribute value-pairs that are specific to the directive. Some important points that are needed to be remembered about the syntax of the directive are as follows:

  • The tag names, their attributes, and their values are all case sensitive.
  • The value must be enclosed within a pair of single or double quotes.
  • A pair of single quotes is equivalent to a pair of double quotes.
  • There must be no space between the equals sign (=) and the value.

A page directive informs the JSP engine about the overall properties of a JSP page. For example, the following page directives inform the JSP engine that Java will be used as scripting language in our JSP page:

<%@ page language=”java” %>

An include directive tells the JSP engine to include the contents of another file (HTML, JSP, etc) into the current file. For example:

<%@ include file=”test.html” %>

             or

<%@ include file=”test.jsp” %>

A taglib directive is used to associate a prefix with a tag library. For example:

<%@ taglib prefix=”test” uri=”taglib.tld” %>

 

2. Declaration Tag:

Declarations declare and define variables and methods that can be used in the JSP page (a JSP declaration can contain any valid Java declaration including inner classes and static code blocks. However, such declarations are rarely used). A declaration always starts with <%! and ends with %>.

For e.g.: <%! int i = 0; %>

This declares an integer variable i and initializes to 0. The variable is initialized only once when the page is first loaded by the JSP engine, and retains its value in subsequent client requests i.e. the value of i is not reset to 0 each time we access the page. It can contain any number of valid Java declaration statements. For example, the following tag declares a variable and a method in a single tag:

<%!
     String name[] = {“biswa”, “amit”, “sreejan”};

    String getName(int i) {
       return name[i];
}
%>

The above declaration can also be written using two separate JSP declaration tags.

 

3. Scriptlet Tag:

Scriptlets are used to embed any Java code fragments in the JSP page.

For example: <% i++; %>     

Here the scriptlet tag is executed and the value of i is incremented each time the page is requested. We can use scriptlets for printing HTML statements also. For e.g.:

<%@ page language="java" %>
<%! int i = 0; %>
<%
out.print("");       
i++;
out.print("The value of i is now:  " + i);
out.print("");
%>
 

4. Expression Tag:

Expression tags are used as a shortcut to print values in the output HTML in a JSP page. Syntax of Expression tag is:

<%= variable %>     

The variable denotes the variable value that is needed to be printed in the output HTML page.   For e.g.: <%= i %>  

The expression is evaluated each time the page is accessed, and its value is then embedded in the output HTML. Unlike variable declarations, expressions must not be terminated with a semicolon. Thus, the following is not valid: <%= i; %>

The below tables denotes some valid and invalid JSP expressions:

 

Valid JSP Expressions:      

Expression

Explanation

<%= 500 %>

An integral literal

<%= anInt*3.5/100-500 %>

An arithmetic expression

<%= aBool %>

A Boolean variable

<%= false %>

A Boolean literal

<%= !false %>

A Boolean expression

<%= getChar() %>

A method returning a char

<%= Math.random() %>

A method returning a double

<%= aVector %>

A variable referring to a Vector object

<%= aFloatObj %>

A method returning a float

<%= aFloatObj.floatValue() %>

A method returning a float

<%= aFloatObj.toString() %>

A method that returns a String object

 

Invalid JSP expressions:

Expression

Explanation

<%= aBool; %>

We cannot use a semicolon in an expression

<%= int i = 20 %>

We cannot define anything inside an expression

<%= sBuff.setLength(12); %>

The method does not return any value. The return type is void

 

 

5. Action Tag:

Action tags are used to provide request–time instructions to the JSP container or JSP engine. There are 7 types of action tags. The following table describes various JSP action tags:

Table 2. JSP Action Tags

JSP Action

Description

Attribute

Description of Attributes

<jsp:forward>

Used to forward a request to a target page

page

Specifies the URL of the

target page

<jsp:include>

Includes a file in

the current JSP

page

page

flush

Specifies the URL of the

resource to be included.

Specifies whether the

buffer should be flushed or

not. The flush value can be

either true or false

<jsp:useBean>

Invokes and

searches for

an existing

bean.

id

class

scope

beanName

Uniquely identifies the instance

of the bean.

Identifies the class from which

the bean objects are to be

implemented.

Defines the scope of the bean.

Defines the referential name for

the bean.

<jsp:getProperty>

Retrieves the

property of a

bean or create a bean into a defined scope

name

property

Defines the name for the

bean.

Defines the property from

which the values are to

be retrieved.

<jsp:setProperty>

Used to set the property for a bean

name

property

value

param

Specifies a name for the

bean.

Defines the property for

which values are to be set.

Defines an explicit value

for the bean property.

Defines the name of the

request parameter to be

used.

<jsp:param>

Defines a

parameter to be

passed to an

included or

forwarded page

name

value

Defines the name of the

reference parameter.

Defines the value of the

specified parameter

<jsp:plugin>

Embed a Java

applets or a

JavaBean

type

code

codebase

Defines the type of plug-in to

be included.

Defines the name of the class

to be executed by the plug-in.

Defines the path of the code

 

The general syntax of a JSP action tag is:

<jsp:actionName attribute-list />

In this tag, actionName is one of the seven actions mentioned and attribute-list represents one or more attribute-value pairs that are specific to the action.

 

 

6. Comment Tag:

Comments are used for documentation purposes but do not affect the output of the JSP page in any way. The syntax of a JSP comment is:

<%-- Anything you want to be commented --%>