0% found this document useful (0 votes)
9 views

JSP Tags

Uploaded by

hacked
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

JSP Tags

Uploaded by

hacked
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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 <%@ directives %>
instructions to the JSP engine.
Declaration Declaration Declares and defines <%! variable dceclaration &
methods and variables. method definition %>
Scriptlet Allows the developer to write free- <% some Java code %>
form Java code in a JSP page.
Expression Used as a shortcut to print values in <%= an Expression %>
the output HTML of a JSP page.
Action Provides request-time instructions to <jsp:actionName />
the JSP engine.
Comment Used for documentation and for <%– any Text –%>
commenting out parts of JSP code.

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 page Specifies the URL of the
request to a target target page
page
<jsp:include> Includes a file in page Specifies the URL of the
the current JSP flush resource to be included.
page Specifies whether the
buffer should be flushed or
not. The flush value can be
either true or false
<jsp:useBean> Invokes and id Uniquely identifies the instance
searches for class of the bean.
an existing scope Identifies the class from which
bean. beanName the bean objects are to be
implemented.
Defines the scope of the bean.
Defines the referential name for
the bean.
<jsp:getProperty> Retrieves the name Defines the name for the
property of a property bean.
bean or create a Defines the property from
bean into a which the values are to
defined scope be retrieved.
<jsp:setProperty> Used to set the name Specifies a name for the
property for a property bean.
bean value Defines the property for
param 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 name Defines the name of the
parameter to be value reference parameter.
passed to an Defines the value of the
included or specified parameter
forwarded page
<jsp:plugin> Embed a Java type Defines the type of plug-in to
applets or a code be included.
JavaBean codebase 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 --%>

You might also like