by suryanarayanajstl jsp standard tag library core, sql, fmt, xml tag libraries by suryanarayana...

32
JSTL JSP Standard Tag Library Core, SQL, FMT, XML Tag Libraries By Suryanarayana By Suryanarayana By Suryanarayana By Suryanarayana ActiveNET Custom tags are used to avoid writing java code in jsp documents. Hence we can substitute them with custom tags. Whereas JSTL allows us to write java logic in JSP document using JSTL tags but not by directly writing Java code. Java logic generally consists of variable declaration, initialization, accessing, storing-retrieving and removing attributes from various scopes (request, session, application), object creation, method calls on object, conditions, loops, exception handling statements, JDBC operations, reading request parameters etc. Additionally we may use I18N support, XML generation, parsing, transformation etc. To perform these operations in JSTL Sun provided 4 tag libraries called: i) Core Tag Library ii) SQL Tag Library iii) XML Tag Library iv) Formatting Tag Library Each tag library consists of many pre-defined tags whose classes are bundled and given in jstl.jar and standard.jar. These tag library classes and their tag names are configured in 4 .tld extension files (c.tld, sql.tld, x.tld, fmt.tld-I18N) Hence to use JSTL in java web project, while creating web project we must include jstl.jar and standard.jar files in project lib directory. Tag Libraries, their files, their namespaces and their prefixes

Upload: others

Post on 11-Mar-2020

38 views

Category:

Documents


4 download

TRANSCRIPT

JSTL

JSP Standard Tag Library

Core, SQL, FMT, XML Tag Libraries

By SuryanarayanaBy SuryanarayanaBy SuryanarayanaBy Suryanarayana

ActiveNET

Custom tags are used to avoid writing java code in jsp documents. Hence we can substitute them

with custom tags.

Whereas JSTL allows us to write java logic in JSP document using JSTL tags but not by directly writing

Java code.

Java logic generally consists of variable declaration, initialization, accessing, storing-retrieving and

removing attributes from various scopes (request, session, application), object creation, method

calls on object, conditions, loops, exception handling statements, JDBC operations, reading request

parameters etc.

Additionally we may use I18N support, XML generation, parsing, transformation etc.

To perform these operations in JSTL Sun provided 4 tag libraries called:

i) Core Tag Library

ii) SQL Tag Library

iii) XML Tag Library

iv) Formatting Tag Library

Each tag library consists of many pre-defined tags whose classes are bundled and given in jstl.jar and

standard.jar. These tag library classes and their tag names are configured in 4 .tld extension files

(c.tld, sql.tld, x.tld, fmt.tld-I18N)

Hence to use JSTL in java web project, while creating web project we must include jstl.jar and

standard.jar files in project lib directory.

Tag Libraries, their files, their namespaces and their prefixes

Tag Library Name tld file name Namespace Prefix

Core Library c.tld http://java.sun.com/jstl/core

http://java.sun.com/jsp/jstl/core

c

SQL Library sql.tld http://java.sun.com/jstl/sql

http://java.sun.com/jsp/jstl/sql

sql

XML Library x.tld http://java.sun.com/jstl/xml

http://java.sun.com/jsp/jstl/xml

x

Formatting Library fmt.tld http://java.sun.com/jstl/fmt

http://java.sun.com/jsp/jstl/fmt

fmt

Functions fn.tld http://java.sun.com/jstl/fn

http://java.sun.com/jsp/jstl/fn

fn

How many tags each tag library consists of:

Tag Name with attributes Explanation

The tags in c.tld are: (14)

Variable management tags:

<c:set var="" value="" scope=""> Store variable with the given name in the given

scope.

<c:out value=""> Retrieves variable from the request scope with

requested variable name.

<c:remove var="" scope=""> Removes variable from the request scope and

name

Conditions tags:

<c:if test=”” var=””> Meant for writing simple/single condition

<c:choose>

<c:when>

<c:otherwise>

</c:choose>

Meant for writing multiple conditions. The whole

complex condition must be place <c:choose> tag,

each condition must be placed in <c:when>, the

last else condition must be placed in

<c:otherwise> tag.

Loops:

<c:forEach >

<c:forTokens >

Exception handling:

<c:catch>

Miscellaneous:

<c:import>

<c:param>

<c:redirect>

<c:param>

<c:url>

<c:param>

The tags in sql.tld are: (6)

<sql:setDataSource>

<sql:transaction>

<sql:update>

<sql:query>

<sql:param>

<sql:dateParam>

The tags in x.tld are: (10)

set

out

choose

when

otherwise

if

forEach

param

parse

transform

The tags in fmt.tld are: (12)

requestEncoding

setLocale

timeZone

setTimeZone

Bundle

setBundle

Message

Param

formatNumber

parseNumber

formatDate

parseDate

The functions in fn.tld are: (15)

fn:contains()

fn:containsIgnoreCase()

fn:endsWith()

fn:escapeXml()

fn:indexOf()

fn:trim()

fn:startsWith()

fn:split()

fn:toLowerCase()

fn:toUpperCase()

fn:substring()

fn:substringAfter()

fn:substringBefore()

fn:length()

fn:replace()

Example on JSTL Core Tag Library Tags

The JSTL Core Tag Library provides tags for variable support, URL management, flow control etc.

+ Open Eclipse

+ Create a DynamicWebProject

Name: jstl_1

Select Add JSTL libraries to WEB-INF/lib folder check box

Click on Finish

+ Create a JSP

Name: Core.jsp

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>

<%-- var management tags --%>

<%-- storing var 'i' into various scope with diff values --%>

<c:set var="i" value="10" />

<!-- by default page scope -->

<c:set var="i" value="100" scope="page" />

<c:set var="i" value="1000" scope="request" />

<c:set var="i" value="10000" scope="session" />

<c:set var="i" value="100000" scope="application" />

<c:set var="str" value="ActiveNET" scope="request" />

<%

Object o = request.getAttribute("i");

out.println(o.getClass().getName() + "<br>");

%>

<%=request.getAttribute("i").getClass().getName()%><br />

<%=request.getAttribute("str").getClass().getName()%><br />

<%-- retrive and display variables from the above scopes --%>

<c:out value="${pageScope.i}" /><br />

<c:out value="${requestScope.i}" /><br />

<c:out value="${sessionScope.i}" /><br />

<c:out value="${applicationScope.i}" /><br />

<%-- removing attribute from a scope --%>

<c:remove var="i" scope="page" />

<c:out value="${pageScope.i}" /><br />

<c:out value="${requestScope.i}" /><br />

<c:out value="${sessionScope.i}" /><br />

<c:out value="${applicationScope.i}" /><br />

<!-- Conditional tags -->

<%-- Simple condition --%>

<c:if test="${5==5}" var="result">

5==5 is <c:out value="${result}" />

<br />

</c:if>

<%-- Compound condition / multiple conditions --%>

<c:choose>

<c:when test="${5==5}">

Yes you know maths<br />

</c:when>

<c:when test="${5!=5}">

you don't know maths<br />

</c:when>

<c:otherwise>

I don't comment on you. <br />

</c:otherwise>

</c:choose>

<%-- Simple Loop / Definite Loop --%>

<c:forEach var="j" begin="1" end="10" step="1">

<c:out value="${j}" /> X 38 = <c:out value="${j*38}" />

<br />

</c:forEach>

<%-- indefinite loop --%>

<%

java.util.List list = new java.util.ArrayList();

list.add("Apple");

list.add("Mango");

list.add("Guava");

list.add("Papaya");

list.add("Grapes");

list.add("Banana");

list.add("Strawberry");

request.setAttribute("fruits", list);

%>

<%-- <c:forEach> tag items attribute takes only array or collection object as its value --%>

<%-- List list=(List)request.getAttribute("fruits"); --%>

<c:forEach items="${requestScope.fruits}" var="fruit">

<c:out value="${fruit}" />

<br />

</c:forEach>

<%-- c:forTokens tag acts like StringTokenizer --%>

<c:forTokens items="I am Mad, i am mad. Are you Mad, We are all mad."

delims=" " var="token">

<c:out value="${token}" />

<br />

</c:forTokens>

<%--

<c:catch var="e">

<%

throw new NullPointerException();

%>

<c:out value="${e}" />

</c:catch>

<c:import url="http://www.activenetinformatics.com"/>

<c:redirect url="http://www.activenetinformatics.com"/>

--%>

<c:url value="http://www.google.com">

<c:param name="hl" value="fr"/>

<c:param name="q" value="JSTL XML Examples"/>

</c:url>

Example on JSTL SQL Tag Library Tags

The SQL tag Library tags are meant to interact with RDBMSs such as Oracle, MySQL, MSSQLServer,

IBM DB2 etc.

+ Create a JSP

Name: SQL.jsp

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>

<%@ taglib uri="http://java.sun.com/jstl/sql" prefix="sql"%>

<PRE>

<FORM method="post" action="./SQLReg.jsp">

Reg ID <input type="text" name="regid"/>

SName <input type="text" name="sname"/>

Email <input type="text" name="email"/>

Mobile <input type="text" name="mobile"/>

Course <input type="text" name="course"/>

Address<input type="text" name="address"/>

<input type="submit" name="submit" value="Reg"/>

</FORM>

</PRE>

<sql:setDataSource driver="sun.jdbc.odbc.JdbcOdbcDriver" url="jdbc:odbc:oracledsn" user="active"

password="activenet" var="con" scope="session"/>

<sql:query dataSource="${sessionScope.con}" sql="select * from reg" var="rs"/>

<TABLE border="1" align="center">

<TR>

<TH>RegID</TH>

<TH>SName</TH>

<TH>Email</TH>

<TH>Mobile</TH>

<TH>Course</TH>

<TH>Address</TH>

</TR>

<c:forEach items="${rs.rows}" var="row">

<TR>

<TD><c:out value="${row.regid}"/></TD>

<TD><c:out value="${row.sname}"/></TD>

<TD><c:out value="${row.email}"/></TD>

<TD><c:out value="${row.mobile}"/></TD>

<TD><c:out value="${row.course}"/></TD>

<TD><c:out value="${row.address}"/></TD>

</TR>

</c:forEach>

</TABLE>

+ Create a JSP

SQLReg.jsp

<%@ taglib uri="http://java.sun.com/jstl/sql" prefix="sql"%>

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>

<sql:transaction dataSource="${sessionScope.con}">

<sql:update sql="INSERT INTO reg VALUES (?,?,?,?,?,?)">

<sql:param value="${param.regid}"/>

<sql:param value="${param.sname}"/>

<sql:param value="${param.email}"/>

<sql:param value="${param.mobile}"/>

<sql:param value="${param.course}"/>

<sql:param value="${param.address}"/>

</sql:update>

</sql:transaction>

<c:redirect url="./SQL.jsp"/>

Type following URL on browser:

http://localhost:8081/jstl_1/SQL.jsp

If we don’t want user to enter/give regid and want to generate regid automatically on server on

our own, write the following JSP.

or Write the following code in SQLReg.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>

<%-- i want to store NewReg.jsp form data into reg table. Next want to display all the records in

tabular format --%>

<sql:transaction dataSource="${orads}">

<%-- PK generation --%>

<c:set var="regid" value="0"/>

<sql:query var="rs" sql="SELECT max(regid) FROM reg"></sql:query>

<c:forEach items="${rs.rows}" var="row">

<c:set var="regid" value="${row.regid}"/>

<c:out value="${regid}"/>

</c:forEach>

<c:out value="${regid}"/>

<%-- record insertion --%>

<sql:update sql="INSERT INTO reg VALUES (?,?,?,?,?,?)" var="i">

<%-- reading request parameters --%>

<sql:param value="${regid+1}"/>

<sql:param value="${param.sname}"/>

<%-- equal to request.getParameter("sname") --%>

<sql:param value="${param.email}"/>

<sql:param value="${param.mobile}"/>

<sql:param value="${param.course}"/>

<sql:param value="${param.address}"/>

</sql:update>

<center>Reg ID is <c:out value="${regid}"/></center><br/>

<%-- displaying all records in HTML tabular format --%>

<TABLE>

<TR>

<TH>RegID</TH>

<TH>SName</TH>

<TH>Email</TH>

<TH>Mobile</TH>

<TH>Course</TH>

<TH>Address</TH>

</TR>

<sql:query sql="SELECT * FROM reg" var="rs1"></sql:query>

<c:forEach items="${rs1.rows}" var="row1">

<TR>

<TD><c:out value="${row1.regid}"/></TD>

<TD><c:out value="${row1.sname}"/></TD>

<TD><c:out value="${row1.email}"/></TD>

<TD><c:out value="${row1.mobile}"/></TD>

<TD><c:out value="${row1.course}"/></TD>

<TD><c:out value="${row1.address}"/></TD>

</TR>

</c:forEach>

</TABLE>

</sql:transaction>

Example on JSTL XML Tag Library:

JSTL XML Tag Library is meant for manipulating and creating XML documents. Before going further

we need to copy the two XML and XPath related libraries into <TOMCAT_HOME>\lib directory.

Download Xalan.jar file from the link http://xml.apache.org/xalan-j/index.html

Download XercesImpl.jar file from http://www.apache.org/dist/xerces/j/

Example on <x:out> tag

The <x:out> tag is used for displaying the result of an xml Path expression and writes the result to

JSP writer object. It is similar to the scriptlet tag <%= %> used in JSP.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

<html>

<head>

<title>XML Tags</title>

</head>

<body>

<h2>Vegetable Information:</h2>

<c:set var="vegetable">

<vegetables>

<vegetable>

<name>onion</name>

<price>40/kg</price>

</vegetable>

<vegetable>

<name>Potato</name>

<price>30/kg</price>

</vegetable>

<vegetable>

<name>Tomato</name>

<price>90/kg</price>

</vegetable>

</vegetables>

</c:set>

<x:parse xml="${vegetable}" var="output"/>

<b>Name of the vegetable is</b>:

<x:out select="$output/vegetables/vegetable[1]/name"/><br>

<b>Price of the Potato is</b>:

<x:out select="$output/vegetables/vegetable[2]/price"/>

</body>

</html>

Example on <x:parse> tag

The <x:parse> tag is used for parse the XML data specified either in the tag body or an attribute. It is

used for parse the xml content and the result will stored inside specified variable.

novels.xml

<books>

<book>

<name>Three mistakes of my life</name>

<author>Chetan Bhagat</author>

<price>200</price>

</book>

<book>

<name>Tomorrow land</name>

<author>NUHA</author>

<price>2000</price>

</book>

</books>

Parse.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

<html>

<head>

<title>x:parse Tag</title>

</head>

<body>

<h2>Books Info:</h2>

<c:import var="bookInfo" url="novels.xml"/>

<x:parse xml="${bookInfo}" var="output"/>

<p>First Book title: <x:out select="$output/books/book[1]/name"/></p>

<p>First Book price: <x:out select="$output/books/book[1]/price"/></p>

<p>Second Book title: <x:out select="$output/books/book[2]/name"/></p>

<p>Second Book price: <x:out select="$output/books/book[2]/price"/></p>

</body>

</html>

Example on <x:set> tag

The <x:set> tag is used to set a variable with the value of an XPath expression. It is used to store the

result of xml path expression in a scoped variable.

Set.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

<html>

<head>

<title>x:set Tag</title>

</head>

<body>

<h3>Books Information:</h3>

<c:set var="book">

<books>

<book>

<name>Three mistakes of my life</name>

<author>Chetan Bhagat</author>

<price>200</price>

</book>

<book>

<name>Tomorrow land</name>

<author>Brad Bird</author>

<price>2000</price>

</book>

</books>

</c:set>

<x:parse xml="${book}" var="output"/>

<x:set var="fragment" select="$output/books/book[2]/price"/>

<b>The price of the Tomorrow land book</b>:

<x:out select="$fragment" />

</body>

</html>

Example on <xml:choose>, <xml:when>, <xml:otherwise>

The <x:choose> tag is a conditional tag that establish a context for mutually exclusive conditional

operations. It works like a Java switch statement in which we choose between a numbers of

alternatives.

The <x:when> is subtag of <x:choose> that will include its body if the condition evaluated be 'true'.

The <x:otherwise> is also subtag of <x:choose> it follows <x:when> tags and runs only if all the prior

condition evaluated is 'false'.

The <x:when> and <x:otherwise> works like if-else statement. But it must be placed inside

<x:choose> tag.

Choose_When_Otherwise.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

<html>

<head>

<title>x:choose Tag</title>

</head>

<body>

<h3>Books Information:</h3>

<c:set var="xmltext">

<books>

<book>

<name>Three mistakes of my life</name>

<author>Chetan Bhagat</author>

<price>200</price>

</book>

<book>

<name>Tomorrow land</name>

<author>Brad Bird</author>

<price>2000</price>

</book>

</books>

</c:set>

<x:parse xml="${xmltext}" var="output"/>

<x:choose>

<x:when select="$output//book/author = 'Chetan bhagat'">

Book is written by Chetan bhagat

</x:when>

<x:when select="$output//book/author = 'Brad Bird'">

Book is written by Brad Bird

</x:when>

<x:otherwise>

The author is unknown...

</x:otherwise>

</x:choose>

</body>

</html>

Example on <x:if> tag

The <x:if> tag is used for evaluating the test XPath expression. It is a simple conditional tag which is

used for evaluating its body if the supplied condition is true.

IfTag.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

<html>

<head>

<title>x:if Tags</title>

</head>

<body>

<h2>Vegetable Information:</h2>

<c:set var="vegetables">

<vegetables>

<vegetable>

<name>onion</name>

<price>40</price>

</vegetable>

<vegetable>

<name>Potato</name>

<price>30</price>

</vegetable>

<vegetable>

<name>Tomato</name>

<price>90</price>

</vegetable>

</vegetables>

</c:set>

<x:parse xml="${vegetables}" var="output"/>

<x:if select="$output/vegetables/vegetable/price < 100">

Vegetables prices are very low.

</x:if>

</body>

</html>

Example on <x:transform> tag

The <x:transform> tag is used in a XML document for providing the XSL (Extensible Stylesheet

Language) transformation. It is used for transforming xml data based on XSLT script.

Transfer.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="doc"/>

<xsl:template match="/">

<html>

<body>

<h2>Company's Employee detail</h2>

<table border="2">

<tr>

<th align="left">Name

</th>

<th align="left">Designation

</th>

<th align="left">Age

</th>

</tr>

<xsl:for-each select="organisation/company/emp">

<tr>

<td>

<xsl:value-of select="name"/>

</td>

<td>

<xsl:value-of select="designation"/>

</td>

<td>

<xsl:value-of select="age"/>

</td>

</tr>

</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

Transfer.xml

<?xml version="1.0" encoding="UTF-8"?>

<organisation>

<company>

<emp>

<name>Rajan Singh</name>

<designation>Bussiness Developer</designation>

<age>40</age>

</emp>

<emp>

<name>Supriya Gaur</name>

<designation>HR Executive</designation>

<age>22</age>

</emp>

</company>

<company>

<emp>

<name>Shashnak Singhal</name>

<designation>Sr. Java Programmer</designation>

<age>26</age>

</emp>

<emp>

<name>Hemant Kishor</name>

<designation>Sr. PHP Programmer</designation>

<age>23</age>

</emp>

</company>

</organisation>

Transform.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

<html>

<head>

<title>x:transform Tag</title>

</head>

</html>

<c:import var="xml" url="transfer.xml" />

<c:import var="xsl" url="transfer.xsl" />

<x:transform xml="${xml}" xslt="${xsl}" />

Example on <x:param> tag

The <x:param> tag is used to set the parameter in the XSLT style sheet. It use along with the

transform tag for sending parameter along with the value.

Transfer.xsl

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="html" indent="yes"/>

<xsl:param name="bgColor"/>

<xsl:template match="/">

<html>

<body>

<xsl:apply-templates/>

</body>

</html>

</xsl:template>

<xsl:template match="books">

<table border="1" width="60%" bgColor="{$bgColor}">

<xsl:for-each select="book">

<tr>

<td>

<b><xsl:value-of select="name"/></b>

</td>

<td>

<xsl:value-of select="author"/>

</td>

<td>

<xsl:value-of select="price"/>

</td>

</tr>

</xsl:for-each>

</table>

</xsl:template>

</xsl:stylesheet>

Param.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

<html>

<head>

<title>x:transform Tag</title>

</head>

<body>

<h3>Novels Information:</h3>

<c:set var="xmltext">

<books>

<book>

<name>Three mistakes of my life</name>

<author>Chetan Bhagat</author>

<price>200</price>

</book>

<book>

<name>Tomorrow land</name>

<author>Brad Bird</author>

<price>1000</price>

</book>

<book>

<name>Wings of fire</name>

<author>Dr. APJ Abdul Kalam</author>

<price>500</price>

</book>

</books>

</c:set>

<c:import url="transfer.xsl" var="xslt"/>

<x:transform xml="${xmltext}" xslt="${xslt}">

<x:param name="bgColor" value="yellow"/>

</x:transform>

</body>

</html>

Formatting (I18N) Tag Library in JSTL:

The formatting tags provide support for message formatting, number and date formatting etc. The

url for the formatting tags is http://java.sun.com/jsp/jstl/fmt and prefix is fmt.

Example on <fmt:parseNumber>

<fmt:parseNumber> tag is used to parses the String representation of a currency, percentage or

number. It is based on the customized formatting pattern.

ParseNumber.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<html>

<head>

<title>fmt:parseNumber tag</title>

</head>

<body>

<h3>The fmt:parseNumber tag Example is:</h3>

<c:set var="Amount" value="786.970" />

<fmt:parseNumber var="j" type="number" value="${Amount}" />

<p><i>Amount is:</i> <c:out value="${j}" /></p>

<fmt:parseNumber var="j" integerOnly="true" type="number" value="${Amount}"

/>

<p><i>Amount is:</i> <c:out value="${j}" /></p>

</body>

</html>

Example on <fmt:timeZone>

The <fmt:timeZone> tag specifies the parsing action nested in its body or the time zone for any time

formatting. It is used for specify the time zone information used for time formatting operations.

TimeZone.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<html>

<head>

<title>fmt:timeZone Tag</title>

</head>

<body>

<c:set var="str" value="<%=new java.util.Date()%>" />

<table border="2" width="100%">

<tr>

<td width="100%" colspan="2" bgcolor="#FF7F50">

<p align="center">

<b>

<font color="#000000" size="6">Formatting:

<fmt:formatDate value="${str}" type="both"

timeStyle="long" dateStyle="long" />

</font>

</b>

</p>

</td>

</tr>

<c:forEach var="zone"

items="<%=java.util.TimeZone.getAvailableIDs()%>">

<tr>

<td width="50%" bgcolor="#C0C0C0">

<c:out value="${zone}" />

</td>

<td width="50%" bgcolor="#FFEBCD">

<fmt:timeZone value="${zone}">

<fmt:formatDate value="${str}" timeZone="${zn}"

type="both"/>

</fmt:timeZone>

</td>

</tr>

</c:forEach>

</table>

</body>

</html>

Example on <fmt:formatNumber>

The <fmt:formatNumber> tag is used to format the numerical value using the specific format or

precision. It is used to format percentages, currencies, and numbers according to the customized

formatting pattern.

FormatNumber.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<html>

<head>

<title>fmt:formatNumber Tag</title>

</head>

<body>

<h3>Formatting of Number:</h3>

<c:set var="Amount" value="9850.14115" />

<p> Formatted Number-1:

<fmt:formatNumber value="${Amount}" type="currency" /></p>

<p>Formatted Number-2:

<fmt:formatNumber type="number" groupingUsed="true" value="${Amount}" /></

p>

<p>Formatted Number-3:

<fmt:formatNumber type="number" maxIntegerDigits="3" value="${Amount}" /><

/p>

<p>Formatted Number-4:

<fmt:formatNumber type="number" maxFractionDigits="6" value="${Amount}" /><

/p>

<p>Formatted Number-5:

<fmt:formatNumber type="percent" maxIntegerDigits="4" value="${Amount}" /></

p>

<p>Formatted Number-6:

<fmt:formatNumber type="number" pattern="###.###$" value="${Amount}" /><

/p>

</body>

</html>

Example on <fmt:parseDate>

The <fmt:parseDate> tag parses the string representation of a time and date. It is used to format the

time and date according to a customized formatting pattern.

ParseDate.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<html>

<head>

<title>fmt:parseDate Tag</title>

</head>

<body>

<h3>Parsed Date:</h3>

<c:set var="date" value="12-08-2016" />

<fmt:parseDate value="${date}" var="parsedDate" pattern="dd-MM-yyyy" />

<p><c:out value="${parsedDate}" /></p>

</body>

</html>

Example on <fmt:bundle>

The <fmt:bundle> tag loads the resource bundle which is used by its tag body. This tag will make the

specified bundle available for all <fmt:message> tags that occurs between the boundary of

<fmt:bundle> and </fmt:bundle> tags.

Simple.java

package com.package;

import java.util.ListResourceBundle;

public class Simple extends ListResourceBundle {

public Object[][] getContents() {

return contents;

}

static final Object[][] contents = { { "colour.Violet", "Violet" },

{ "colour.Indigo", "Indigo" }, { "colour.Blue", "Blue" }, };

}

Bundle.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<html>

<head>

<title>fmt:bundle Tag</title>

</head>

<body>

<fmt:bundle basename="com.ActiveNET.Simple" prefix="colour.">

<fmt:message key="Violet"/><br/>

<fmt:message key="Indigo"/><br/>

<fmt:message key="Blue"/><br/>

</fmt:bundle>

</body>

</html>

Example on <fmt:setTimeZone>

The <fmt:setBundle> tag is used to load the resource bundle and store their value in the bundle

configuration variable or the name scope variable.

Example on <fmt:setBundle>

The <fmt:setBundle> tag is used to load the resource bundle and store their value in the bundle

configuration variable or the name scope variable.

Main.java

package com.example;

import java.util.ListResourceBundle;

public class Main extends ListResourceBundle {

public Object[][] getContents() {

return contents;

}

static final Object[][] contents = { { "vegetable.Potato", "Potato" },

{ "vegetable.Tomato", "Tomato" }, { "vegetable.Carrot", "Carrot" }, };

}

SetBundle.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<html>

<head>

<title>fmt:setBundle Tag</title>

</head>

<body>

<fmt:setBundle basename="com.ActiveNET.Main" var="lang"/>

<fmt:message key="vegetable.Potato" bundle="${lang}"/><br/>

<fmt:message key="vegetable.Tomato" bundle="${lang}"/><br/>

<fmt:message key="vegetable.Carrot" bundle="${lang}"/><br/>

</body>

</html>

Example on <fmt:message>

The <fmt:message> tag is used for displaying an internationalized message. It maps the key of

localized message to return the value using a resource bundle specified in the bundle attribute.

Message.java

package com.example;

import java.util.ListResourceBundle;

public class Message extends ListResourceBundle {

public Object[][] getContents() {

return contents;

}

static final Object[][] contents = { { "vegetable.Potato", "Potato" },

{ "vegetable.Tomato", "Tomato" }, { "vegetable.Carrot", "Carrot" }, };

}

Message.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<html>

<head>

<title>fmt:message Tag</title>

</head>

<body>

<fmt:setBundle basename="com.ActiveNET.Message" var="lang"/>

<fmt:message key="vegetable.Potato" bundle="${lang}"/><br/>

<fmt:message key="vegetable.Tomato" bundle="${lang}"/><br/>

<fmt:message key="vegetable.Carrot" bundle="${lang}"/><br/>

</body>

</html>

Example on <fmt:formatDate>

The <fmt:formatDate> tag is used for different formats of date and time using the supplied pattern

and styles. It is used to format the time and date according to the customized formatting pattern.

FormatDate.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<html>

<head>

<title>fmt:formatDate</title>

</head>

<body>

<h2>Different Formats of the Date</h2>

<c:set var="Date" value="<%=new java.util.Date()%>" />

<p>

Formatted Time :

<fmt:formatDate type="time" value="${Date}" />

</p>

<p>

Formatted Date :

<fmt:formatDate type="date" value="${Date}" />

</p>

<p>

Formatted Date and Time :

<fmt:formatDate type="both" value="${Date}" />

</p>

<p>

Formatted Date and Time in short style :

<fmt:formatDate type="both" dateStyle="short" timeStyle="short"

value="${Date}" />

</p>

<p>

Formatted Date and Time in medium style :

<fmt:formatDate type="both" dateStyle="medium" timeStyle="medium"

value="${Date}" />

</p>

<p>

Formatted Date and Time in long style :

<fmt:formatDate type="both" dateStyle="long" timeStyle="long"

value="${Date}" />

</p>

</body>

</html>

Functions Library

JSTL fn:contains() function

The fn:contains() is used for testing if the string containing the specified substring. If the specified

substring is found in the string, it returns true otherwise false.

Contains.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>

<head>

<title>Using JSTL Functions</title>

</head>

<body>

<c:set var="String" value="Welcome to ActiveNET"/>

<c:if test="${fn:contains(String, 'ActiveNET')}">

<p>Found ActiveNET string<p>

</c:if>

<c:if test="${fn:contains(String, 'ACTIVENET')}">

<p>Found ACTIVENET string<p>

</c:if>

</body>

</html>

JSTL fn:containsIgnoreCase() function

The fn:containsIgnoreCase() function is used to test if an input string contains the specified substring

as a case insensitive way. During searching the specified substring it ignores the case.

ContainsIgnoreCase.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>

<head>

<title>Using JSTL Functions</title>

</head>

<body>

<c:set var="String" value="Welcome to javatpoint"/>

<c:if test="${fn:containsIgnoreCase(String, 'javatpoint')}">

<p>Found javatpoint string<p>

</c:if>

<c:if test="${fn:containsIgnoreCase(String, 'JAVATPOINT')}">

<p>Found JAVATPOINT string<p>

</c:if>

</body>

</html>

JSTL fn:endsWith() function

The fn:endsWith() function is used for testing if an input string ends with the specified suffix. If the

string ends with a specified suffix, it returns true otherwise false.

EndsWith.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>

<head>

<title>Using JSTL Functions</title>

</head>

<body>

<c:set var="String" value="Welcome to JSP programming"/>

<c:if test="${fn:endsWith(String, 'programming')}">

<p>String ends with programming<p>

</c:if>

<c:if test="${fn:endsWith(String, 'JSP')}">

<p>String ends with JSP<p>

</c:if>

</body>

</html>

JSTL fn:escapeXml() function

EscapeXml.jsp

The fn:escapeXml() function escapes the characters that would be interpreted as XML markup. It is

used for escaping the character in XML markup language.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>

<head>

<title>Using JSTL Functions</title>

</head>

<body>

<c:set var="string1" value="It is first String."/>

<c:set var="string2" value="It is <xyz>second String.</xyz>"/>

<p>With escapeXml() Function:</p>

<p>string-1 : ${fn:escapeXml(string1)}</p>

<p>string-2 : ${fn:escapeXml(string2)}</p>

<p>Without escapeXml() Function:</p>

<p>string-1 : ${string1}</p>

<p>string-2 : ${string2}</p>

</body>

</html>

JSTL fn:indexOf() function

The fn:indexOf() function return an index of string. It is used for determining the index of string

specified in substring.

IndexOfjsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>

<head>

<title>Using JSTL Functions</title>

</head>

<body>

<c:set var="string1" value="It is first String."/>

<c:set var="string2" value="It is <xyz>second String.</xyz>"/>

<p>Index-1 : ${fn:indexOf(string1, "first")}</p>

<p>Index-2 : ${fn:indexOf(string2, "second")}</p>

</body>

</html>

JSTL fn:trim() function

The fn:trim() function removes the blank spaces from both the ends of a string. It mainly used for

ignoring the blank spaces from both the ends of string.

Trim.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>

<head>

<title>Using JSTL Functions</title>

</head>

<body>

<c:set var="str1" value="Welcome to JSP programming "/>

<p>String-1 Length is : ${fn:length(str1)}</p>

<c:set var="str2" value="${fn:trim(str1)}" />

<p>String-2 Length is : ${fn:length(str2)}</p>

<p>Final value of string is : ${str2}</p>

</body>

</html>

JSTL fn:startsWith() function

The fn:startsWith() function test if an input string is started with the specified substring.

StartsWith.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>

<head>

<title>Using JSTL Function</title>

</head>

<body>

<c:set var="msg" value="The Example of JSTL fn:startsWith() Function"/>

The string starts with "The": ${fn:startsWith(msg, 'The')}

<br>The string starts with "Example": ${fn:startsWith(msg, 'Example')}

</body>

</html>

JSTL fn:split() function The fn:split() function splits the string into an array of substrings. It is used for splitting the string into

an array based on a delimiter string.

Split.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>

<head>

<title>Using JSTL Functions</title>

</head>

<body>

<c:set var="str1" value="Welcome-to-JSP-Programming."/>

<c:set var="str2" value="${fn:split(str1, '-')}" />

<c:set var="str3" value="${fn:join(str2, ' ')}" />

<p>String-3 : ${str3}</p>

<c:set var="str4" value="${fn:split(str3, ' ')}" />

<c:set var="str5" value="${fn:join(str4, '-')}" />

<p>String-5 : ${str5}</p>

</body>

</html>

JSTL fn:toLowerCase() function

The fn:toLowerCase() function converts all the characters of a string to lower case. It is used for

replacing any upper case character in the input string with the corresponding lowercase character.

ToLowerCase.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>

<head>

<title> Using JSTL Function </title>

</head>

<body>

<c:set var="string" value="Welcome to JSP Programming"/>

${fn:toLowerCase("HELLO,")}

${fn:toLowerCase(string)}

</body>

</html>

JSTL fn:toUpperCase() function

The fn:toUpperCase() function converts all the characters of a string to upper case. It is used for

replacing any lower case character in the input string with the corresponding upper case character.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>

<head>

<title>Using JSTL Function </title>

</head>

<body>

<c:set var="site" value="javatpoint.com"/>

<c:set var="author" value="Sonoo Jaiswal"/>

Hi, This is ${fn:toUpperCase(site)} developed by ${fn:toUpperCase(author)}.

</body>

</html>

JSTL fn:substring() function The fn:substring() function returns the subset of a string. It is used to return the substring of given

input string according to specified start and end position.

SubString.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>

<head>

<title>Using JSTL Function </title>

</head>

<body>

<c:set var="string" value="This is the first string."/>

${fn:substring(string, 5, 17)}

</body>

</html>

JSTL fn:substringAfter() function

The fn:substringAfter() function returns the subset of string followed by a specific substring. It

returns the part of string which lies after the provided string value.

SubStringAfter.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>

<head>

<title>Using JSTL Function </title>

</head>

<body>

<c:set var="string" value="Nakul Jain"/>

${fn:substringAfter(string, "Nakul")}

</body>

</html>

JSTL fn:substringBefore() function The fn:substringBefore() function returns the subset of string before a specific substring. It is used

for returning a part of original string which lies before the specified string value.

SubStringBefore.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>

<head>

<title>Using JSTL Function </title>

</head>

<body>

<c:set var="string" value="Hi, This is JAVATPOINT.COM developed by SONOO JAISWAL.

"/>

${fn:substringBefore(string, "developed")}

</body>

</html>

JSTL fn:length() function The fn:length() function returns the number of characters inside a string, or the number of items in a

collection. It is used for calculating the length of string and to find out the number of elements in a

collection.

Length.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>

<head>

<title>JSTL fn:length() example</title>

</head>

<body>

<c:set var="str1" value="This is first string"/>

<c:set var="str2" value="Hello"/>

Length of the String-1 is: ${fn:length(str1)}<br>

Length of the String-2 is: ${fn:length(str2)}

</body>

</html>

JSTL fn:replace() function The fn:replace() function replaces all the occurrence of a string with another string sequence. It

search in an input string and replace it with the provided string.

Replace.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>

<head>

<title>Using JSTL Function </title>

</head>

<body>

<c:set var="author" value="Ramesh Kumar"/>

<c:set var="string" value="pqr xyz abc PQR"/>

${fn:replace(author, "Ramesh", "Suresh")}

${fn:replace(string, "pqr", "hello")}

</body>

</html>