JSP Tags
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:
Note: Here, by using comment tag, example of five JSP tags are shown.
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 %>.
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:
An include directive tells the JSP engine to include the contents of another file (HTML, JSP,
etc) into the current file. For example:
or
A taglib directive is used to associate a prefix with a tag library. For example:
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 %>.
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.
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.:
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:
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
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:
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: