JSTL Tutorial
JSTL tutorial explains you the various JSP tags that can be used in the JSP pages.
JSTL tutorial explains you the various JSP tags that can be used in the JSP pages.
JSTL Tutorial
In this section we will read about JSTL. You will read about how it is
implemented and used in JSP pages.
JSTL also known as Java Standard Tag Library is a collection of various tags
for performing tasks like iteration, conditional checks, data formatting,
localization etc. These tags are gathered as to perform the tasks that are
commonly used in JSP applications. JSTL provides the feature for integrating the
custom tags with JSTL tags. JSTL tags are classified on their use/purpose. These
are as follows :
- JSTL Core Tags
- JSTL Formatting Tags
- JSTL SQL Tags
- JSTL XML Tags
- JSTL Functions
To use JSTL you would have to first download the JSTL JARs. Then it should be
kept inside the lib folder in the web server/application server.
JSTL Core Tags
JSTL Core Tags are used for manipulating with the data in JSP pages. Such as,
iterating on data collection, set an output, apply the conditional statements,
set the parameter etc.
To use the JSTL Core Tags you would have to use the following taglib
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
Followings are the commonly used tags of JSTL core tag library that are used
in the JSP pages :
- <c:out > : This tag is used for displaying the output. This
tag is like the <%= %> expressions in JSP for showing the output.
- <c:if> : This tag is like the "if" conditional statement in
Java which evaluates its body when the given condition becomes true.
- <c:choose> : This tag is the super tag of <c:when> tag.
choose specifies the mutually exclusive conditional operations.
- <c:when> : This tag is the sub tag of <c:choose> tag. This
tag evaluates its body when the given condition becomes true.
- <c:otherwise > : This tag is the sub tag of <c:choose> tag.
This tag evaluates its body when all the condition given earlier becomes false.
- <c:import> : This tag is used to include the functionality
of the specified URL as an absolute or relative and may put it into either a
page, a string var, or a Reader.
- <c:forEach > : This tag is used to iterate over the
collection of data.
- <c:url> : This tag is used for URL rewriting.
- <c:param> : This tag is used to provide the additional
parameter to the URL.
- <c:redirect > : This tag is used to redirect to a specified
URL location.
- <c:set> : This tag is used to set the outcome after
evaluation of an expression.
- <c:remove> : This tag is used to remove a variable
specified in the particular scope.
- <c:catch> : This tag is used to catch the throwable
exception.
- <c:forTokens> : This tag is used for traversing over tokens
for the given delimeters.
JSTL Formatting Tags
JSTL Formatting Tags are useful for the internationalization.
Internationalization means formatting the text, date, time, numbers, etc.
according to their localization. The JSTL format tag library provides many of
the tags that are helpful in creation of internationalized websites.
To use the JSTL Formatting Tags you would have to use the following taglib :
<%@ taglib prefix="fmt"
uri="http://java.sun.com/jsp/jstl/fmt" %>
Followings are the commonly used tags of JSTL format tag library :
- <fmt:formatNumber> : This tag is used to format a numeric
value in a specified format.
- <fmt:parseNumber> : This tag is used to parse the number
represented as string, currency, or percentage.
- <fmt:formatDate> : This tag is used to format a date/time
into the specified format.
- <fmt:parseDate> : This tag is used to parse the date
represented as string.
- <fmt:bundle> : This tag is used to load the resource
bundle.
- <fmt:setLocale> : This tag is used to set the specified
locale.
- <fmt:setBundle> : This tag is used to load the resource
bundle and keep it into the scope variable.
- <fmt:message> : This tag is used to format the message as
an internationalized message.
- <fmt:timeZone> : This tag is used to format the time zone.
- <fmt:setTimeZone> : This tag is used to specifies the time
zone.
- <fmt:requestEncoding> : This tag is used to specifies the
request character encoding.
JSTL SQL Tags
JSTL SQL Tags are useful to manipulate the database data in the JSP page.
These tags helps in to interact with the Database for example, MySQL, Oracle,
etc.
To use the JSTL SQL Tags you would have to use the following taglib :
<%@ taglib prefix="sql"
uri="http://java.sun.com/jsp/jstl/sql" %>
Followings are the commonly used tags of JSTL sql tag library :
- <sql:setDataSource> : This tag is used for creating data
source.
- <sql:query> : This tag is used to execute the sql query
defined inside its body or by the sql attribute.
- <sql:update> : This tag is used to execute the sql update
defined inside its body or by the sql attribute.
- <sql:param> : This tag is used to add a parameter to the
sql statement to the given value.
- <sql:dateParam> : This tag is used to add a parameter to
the sql statement to the given java.util.Date value.
- <sql:transaction > : This tag is used to execute all
statements in one transaction.
JSTL XML Tags
JSTL XML Tags are useful for manipulating the XML documents. This tag library
allows the developer to interact with the XML data on JSP page for example, XML
parsing, transform XML data, flow control based on XPath expressions.
To use the JSTL XML Tags you would have to use the following taglib :
<%@ taglib prefix="x"
uri="http://java.sun.com/jsp/jstl/xml" %>
When you want to work with the JSTL XML Tag libraries you would also have to
keep the two additional XML and XPath related libraries inside the lib folder of
the Tomcat Installation Directory.
Followings are the commonly used tags of JSTL XML tag library :
- <x:out> : This
tag is like the <%=..... %> XPath expression.
- <x:parse> : This tag is used to parse the XML data.
- <x:set> : This tag is used to set a variables value of
XPath expression.
- <x:if> : This tag is processes its body when the XPath
expression is true.
- <x:forEach> : This tag is used to traverse over the nodes
in an XML document.
- <x:choose> : This tag is the super tag of <x:when> tag.
choose specifies the mutually exclusive conditional operations
- <x:when> : This tag is the sub tag of <x:choose> tag. This
tag evaluates its body when the given condition becomes true.
- <x:otherwise> : This tag is the sub tag of <x:choose> tag.
This tag evaluates its body when all the condition given earlier becomes false.
- <x:transform> : This tag is used to transform XSL data on
XML document.
- <x : param> : This tag is used to add the parameter in XSL
style sheet. This tag is used with transform tag.
JSTL Functions
JSTL Functions library is the collection of functions that are provided to
use. One can use these standard functions on the JSP pages while coding. Most of
these provided functions are commonly used for string manipulation.
To use the JSTL functions you would have to use the following taglib :
<%@ taglib prefix="fn"
uri="http://java.sun.com/jsp/jstl/functions" %>
Followings are the commonly used functions of JSTL functions :
- fn:contains() : This method is used to check whether the
specified string is contained by the existing string or not. This method is case
sensitive.
- fn:containsIgnoreCase() : This method is used to check
whether the specified string is contained by the existing string or not. This
method is case insensitive.
- fn:endsWith() : This method is used to check whether the
specified string is ends with the specified suffix or not.
- fn:escapeXml() : This method is used to escape the XML
characters/tags.
- fn:indexOf() : This method is used to get the index of the
first occurrence of the string within a string.
- fn:join() : This method is used to join the elements of
array into a string.
- fn:length() : This method is used to return the size of the
string.
- fn:replace() : This method is used to replace an existing
string with the new one string.
- fn:split() : This method is used to split a string and
returns an array of substrings.
- fn:startsWith() : This method is used to check whether the
specified string is started with the given prefix or not.
- fn:substring() : This method is used to find the substring
of specified string.
- fn:substringAfter() : This method is used to find the
substring after a given substring.
- fn:substringBefore() : This method is used to find the
substring before a given substring.
- fn:toLowerCase() : This method is used to convert the upper
case string to lower case.
- fn:toUpperCase() : This method is used to convert the lower
case string to upper case.
- fn:trim() : This method is used to remove the white space
from the both side of string.
To read more in detail about JSTL and Examples you may go through the
following link : http://www.roseindia.net/jstl/index.shtml
Ads