Wednesday, May 15, 2013

JSTL (Jsp Standard Tag Library) tags and their use

With the introduction of custom tags to remove the java code from jsp, every vendor can develop their own tags. But the problem is that, for every project in every organisation some common requirements will exists. For those, it's waste of time to develop own tags by every vendor. Instead of that Sun itself has given commonly used tags to use in our applications.


JSTL refers to Jsp Standard Tag Library and is a custom tag library released by Sun Micro Systems.

JSTL comes with different set of groups of tags. They are

core Tag library,
fmt tag library,
sql tag library and
xml tag library

Core Tag Library:

If we want to use core tag library in our jsp instead of java code, we need to include the tag library as shown.

prefix attribute can take any characters to use the tags in jsp. uri attribute specifies the uri of the corresponding tag library, add the jstl jar in the lib folder.

As part of Core tag library the following tags are bundled.


<c:set> is used to set a value into a specific scope or to set the result of an expression into a specific scope.


value attribute allows any value or result of any expression as shown above.
scope can be any of page, request, session and application.


<c:redirect> is used to redirect to a specified location. Instead of response.sendRedirect() ( java code ) we can use <c:redirect>.


url attribute specifies the location to which it has to be redirected.


<c:out> is used to display the contents to the user (on the browser).Instead of out.println() (java code) we can use <c:out> in jsp.


value attribute specifies the value to be rendered on the browser, default attribute will take the default value to be display if the resulting value is null.


<c:catch> is used to catch the exception thrown in its body.


var attribute specifies the name of the scoped variable for the exception thrown.


<c:if> is the simple conditional tag to check conditions in jsp.


test is the required attribute here, which allows a condition check , and executes the if body incase of condition is true.

var is the name of the variable for the condition result and of type boolean (true for condition true and false for condition fail)


<c:choose> is the simple conditional tag which has child tags <c:when> and <c:otherwise>



when the condition given in the <c:choose> is true it's body will be executed , if condiction fails it will skip executing body and go for <c:otherwise>. test is the mandatory attribute where we will provide condition.


when condition fails in the <c:when>, then the <c:otherwise> body will be executed just like if ,else in java code.


To perform the iterations, we will take the help of <c:forEach> which acts as for loop in jsp.


begin and end specifies the starting and ending value of the loop to iterate and step specifies the incremental value from starting. if we have not given begin and end,and if we mentioned the items attribute to iterate over collection elements, index always starts from 0.


<c:forTokens> is used to iterate over set of tokens seperated by delimeter


items specifies collection of items seperated by delimeter. here @ is considered as delimeter , we can use any character as delimter (including space).


<c:import> is used to import the contents from specified url (just like jsp:include)


or if we want to include html content also we can use like this,


To create url with optional parameters we will use <c:url>


with the given var name we can access the url in the jsp later.


<c:param> is used to add parameters to the url that will be given to import tag


The parameters mentioned will be appended to url while importing.


to remove a specific scoped variable from a specific scope <c:remove> will be used.


after removing from specified scope, if we try to access that variable we will get null as it was removed from the scope.

fmt tag library:

fmt (formatting) tags used to format the text, numbers, date based on the locale (internationalization).

To use fmt tag library we need to include taglib,

some mostly used tags are., 


this tag is used to retrieve the message from property file based on the key and bundle


formatDate and formatNumber:

Used to format given date and number based on supplied style/ pattern.


Similar way the other tags u can go through from the link.

Sql tag library:

sql tag library is used to deal with database opeartions. To set the datsource we have <sql:setDataSource>, to execute a query <sql:query>.

To Executes the SQL update defined in its body we can use <sql:update>


As per MVC design pattern, we should not write dao logic in jsp. So in general we will not use these tags at all.

Xml tag library:

xml tag library is used to deal with xml documents and almost all tags available as part of xml tag library are same as core tag library.

You can go through this link.

Download the source and observe the working behaviour of all tags.

1 comment: