20 - JSP Standard Tag Library (JSTL): Page 7 of 7

20.4 XML Tag library

XML tag library of JSTL provides a way to create and manipulate XML data and provides several tags.

· URI for xml tag library is http://java.sun.com/jsp/jstl/xml

· To use xml tag library on JSP page we need to import its tag library using

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

20.4.1 <x:parse>

 This tag is used to parse XML data. Two main attributes of this tag are

a)var- variable to store parsed xml data

b)xml- scoped variable containing input xml data

<x:parse var=”variable_name” xml=”variable_holds_input_xml_data”/>

Example:

Lets create xparse.jsp which will parse the xml data provided in jsp itself.

<html>
  <head>
    <title> x:parse tag example </title>
  </head>
  <%@ taglib prefix="x"uri="http://java.sun.com/jsp/jstl/xml" %>
  <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>
  <body>
    <h3>Person Data</h3>
    <c:setvar="xmlData">
    <persons>
      <person>
        <name>Joe Bloggs</name>
        <address>United States</address>
        <dob>12-28-1978</dob>
      </person>
      <person>
        <name>Scott tiger</name>
        <address>United Kingdom</address>
        <dob>11-13-1956</dob>
      </person>
    </persons>
    </c:set>
    <x:parse xml="${xmlData}"var="parsedXml"/>
  </body>
</html>

20.4.2 <x:out>

This tag is similar to expressions but is used for XPath Expressions. Following are the attributes of this tag-

a)select- xpath expression

b)escapeXml- is an optional attribute and can be used to escape the special characters. Default value of this tag is true.

<x:out select=”xpath-expression” escapeXml=”true|false”/>

Example:

Lets create xout.jsp which will parse the xml data provided in jsp itself and prints the data of it.

<html>
<head>
<title> x:out tag example </title>
</head>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>
<body>
    <h3>Person Data</h3>
<c:setvar="xmlData">
<persons>
<person>
<name>Joe Bloggs</name>
<address>United States</address>
<dob>12-28-1978</dob>
</person>
<person>
<name>Scott tiger</name>
<address>United Kingdom</address>
<dob>11-13-1956</dob>
</person>
</persons>
</c:set>
<x:parse xml="${xmlData}" var="parsedXml"/>
Name of 1st Person is - <x:out select="$parsedXml/persons/person[1]/name" />
<br/>
Address of 1st Person is - <x:out select="$parsedXml/persons/person[1]/address" />
<br/>
DOB of 1st Person is - <x:out select="$parsedXml/persons/person[1]/dob" />
<br/>
Name of 2nd Person is - <x:out select="$parsedXml/persons/person[2]/name" />
<br/>
Address of 2nd Person is - <x:out select="$parsedXml/persons/person[2]/address" />
<br/>
DOB of 2nd Person is - <x:out select="$parsedXml/persons/person[2]/dob" />
</body>
</html>

Access xout.jsp by hitting http://localhost:8080/jsp-tutorial/xout.jsp

20.4.3 <x:set>

This tag is used to store a value of any xpath expression in any scope for the later use. Following are the attributes of this tag-

a)select- xpath expression

b)var- variable to store the xpath expression result.

c) scope- scope in which variable will be stored. Default value is page and possible values are page, request, session and application.

<x:set var=”variable_name” select=”xpath-expression” scope=”page|session|application|request”/>

Lets create xset.jsp which will parse the xml data provided in jsp itself and sets the result of xpath expression.

<html>
<head>
<title> x:settag example </title>
</head>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>
<body>
    <h3>Person Data</h3>
<c:setvar="xmlData">
<persons>
<person>
<name>Joe Bloggs</name>
<address>United States</address>
<dob>12-28-1978</dob>
</person>
<person>
<name>Scott tiger</name>
<address>United Kingdom</address>
<dob>11-13-1956</dob>
</person>
</persons>
</c:set>
<x:parse xml="${xmlData}" var="parsedXml"/>
<x:setvar="personName"  select="$parsedXml/persons/person[1]/name" />
<br/>
Name of Person is  <x:out select="$personName" />
<br/>
</body>
</html>

Access xset.jsp by hitting http://localhost:8080/jsp-tutorial/xset.jsp

20.4.4 <x:forEach>

 This tags is similar to loops in java and used for iteration over a xml data.

Following attributes can be used with this tag

a)var- counter variable name

b)select- xpath expression on which iteration has to perform

c)beginis the initial counter value.

d)end- last value or limit

e)step- increment to be done in counter value after each run

f)varStatus- variable to expose the loop status

Lets create xforEach.jsp to demonstrate the <x:forEach> tag like below

<html>
<head>
<title> x:forEach tag example </title>
</head>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>
<body>
<c:setvar="xmlData">
<persons>
<person>
<name>Joe Bloggs</name>
<address>United States</address>
<dob>12-28-1978</dob>
</person>
<person>
<name>Scott tiger</name>
<address>United Kingdom</address>
<dob>11-13-1956</dob>
</person>
</persons>
</c:set>
<x:parse xml="${xmlData}" var="parsedXml"/>
<x:forEach select="$parsedXml/persons/person/name" var="person">
   Person Name: <x:out select="$person" />
<br/>
</x:forEach>
</body>
</html>

Access xforEach.jsp by hitting http://localhost:8080/jsp-tutorial/xforEach.jsp

20.4.5 <x:choose> , <x:when> , <x:otherwise> 

These tags are similar to switch statement with default case in java but here for xml data

Consider <x:choose> as a switch , multiple <x:when> as case and <x:otherwise> as default which means we can have a multiple <x:when> and single <x:when> in a <x:choose> tag.

Syntax is like below –

<x:choose>
<x:whenselect=”” />
</x:when>
<x:when select=”” />
</x:when>
<x:otherwise>
</x:otherwise>
</x:choose>

<x:choose> and <x:otherwise> do not have any attributes but <x:when> has a mandatory “select” attribute

Lets create xchoose.jsp to demonstrate the <x:choose> tag like below

<html>
<head>
<title> x:choose tag example </title>
</head>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>
<body>
<c:setvar="xmlData">
<persons>
<person>
<name>Joe Bloggs</name>
<address>United States</address>
<dob>12-28-1978</dob>
<salary>4500</salary>
</person>
<person>
<name>Scott tiger</name>
<address>United Kingdom</address>
<salary>1500</salary>
</person>
</persons>
</c:set>
<x:parse xml="${xmlData}" var="parsedXml"/>
<x:choose>
<x:when select="$parsedXml/persons/person[1]/salary > 2000">
      Salary is more than $2000
</x:when>
<x:when select="$parsedXml/persons/person[1]/salary < 2000">
       Salary is less than $2000
</x:when>
<x:otherwise>
      Cannot calculate 
</x:otherwise>
</x:choose>
</body>
</html>

Access xchoose.jsp by hitting http://localhost:8080/jsp-tutorial/xchoose.jsp

20.4.6 <x:if>

The <x:if> tag is used to implement conditions and is equivalent to if conditions in java. If the condition evaluates to true then the block gets executed.

Syntax of this tag is

     <x:if select=”condition"> . . . </c:if>if>

Optionally there are two other attributes var and scope can be used. In this case , result of condition gets stored in a scope with variable name.

    <x:if select="condition" var=”variable_name” scope=”page | request | application| session”>pplication| session”>

Lets create xif.jsp to demonstrate the <x:if> tag like below.

<html>
<head>
<title> x:if tag example </title>
</head>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>
<body>
<c:setvar="xmlData">
<persons>
<person>
<name>Joe Bloggs</name>
<address>United States</address>
<dob>12-28-1978</dob>
<salary>4500</salary>
</person>
<person>
<name>Scott tiger</name>
<address>United Kingdom</address>
<salary>1500</salary>
</person>
</persons>
</c:set>
<x:parse xml="${xmlData}" var="parsedXml"/>
<x:if select="$parsedXml/persons/person[1]/salary > 2000">
      Salary is more than $2000
</x:if>
<x:ifvar="result" select="$parsedXml/persons/person[1]/salary > 2000"/>
<br/>
   Is Salary more than $2000 :: <c:out value="${result}"></c:out>
</body>
</html>

Access xif.jsp by hitting http://localhost:8080/jsp-tutorial/xif.jsp

20.5 JSTL functions

JSTL provides several functions to manipulate the strings.

· URI for functions tag is http://java.sun.com/jsp/jstl/functions

· To use functions tag library on JSP page we need to import its tag library using

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

20.5.1 fn:contains()

This function is used to determine if the given string is present as a substring in main string or not. This function will return boolean value (true or false)

Syntax of the function is :

boolean fn:contains(String inputstring, String substring)

Example

create fncontains.jsp to demonstrate the fn:contains function like below.

<%@ tagliburi="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ tagliburi="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>fn:contains example</title>
</head>
<body>
<c:setvar="inputString" value="Hello I am Input String"/>
<c:setvar="subString" value="am" />
<c:if test="${fn:contains(inputString, subString)}">
<c:out value="Input String contains 'am' "/>
</c:if>
</body>
</html>

Access fncontains.jsp by hitting http://localhost:8080/jsp-tutorial/fncontains.jsp

20.5.2 fn:containsIgnoreCase()

This function is used to determine if the given string is present as a substring in main string or not. As compared to ‘contains’ function, this functions ignores case. This function will return boolean value (true or false)

Syntax of the function is :

boolean fn:containsIgnoreCase(String inputstring, String substring)

Example

create fnContainsIgnoreCase.jsp to demonstrate the fn:containsIgnoreCase function like below.

<%@ tagliburi="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ tagliburi="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>fn:containsIgnoreCase example</title>
</head>
<body>
<c:setvar="inputString" value="Hello I am Input String"/>
<c:setvar="subString" value="Am" />
<c:if test="${fn:containsIgnoreCase(inputString, subString)}">
<c:out value="Input String contains 'Am' "/>
</c:if>
</body>
</html>

Access fnContainsIgnoreCase.jsp by hitting http://localhost:8080/jsp-tutorial/fnContainsIgnoreCase.jsp

20.5.3 fn:indexOf()

This function is used to determine the start(first) index of if the given string in main string or not. This function is case sensitive.

This functions returns -1 in case string is not found

Syntax of the function is :

int fn:indexOf(String inputstring, String substring)

Example

create fnindexOf.jsp to demonstrate the fn:indexOf function like below.

<%@ tagliburi="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ tagliburi="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>fn:indexOf example</title>
</head>
<body>
<c:setvar="inputString" value="Hello I am Input String"/>
<c:setvar="subString" value="am" />
<c:setvar="example1" value="${fn:indexOf(inputString, subString)}" />
<c:setvar="example2" value="${fn:indexOf(inputString, 'Am')}" />
Index of String '${subString}'  in ${inputString} is  <c:out value="${example1}"/>
<br/>
 Index of String 'Am'  in ${inputString} is  <c:out value="${example2}"/>
</body>
</html>

Access fnindexOf.jsp by hitting http://localhost:8080/jsp-tutorial/fnindexOf.jsp

20.5.4 fn:escapeXml()

This function is used to escape the some special characters like

< is converted to <

> is converted to >

& is converted to &

’ is converted to '

" is converted to ";

If the string is escaped , then browser does not treat it as a markup and so displays as-is.

Syntax of the function is :

String fn:escapeXml(String inputstring)

Example

create fnescapeXml.jsp to demonstrate the fn:escapeXml function like below.

<%@ tagliburi="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ tagliburi="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>fn:escape example</title>
</head>
<body>
Input String with escapeXml :: ${fn:escapeXml('<i>Hello <strong>I am </strong> Input String<i>')}
<br/>
Input String without escapeXml :: ${'<i>Hello <strong>I am </strong> Input String<i>'}
</body>
</html>

Access fnescapeXml.jsp by hitting http://localhost:8080/jsp-tutorial/fnescapeXml.jsp

20.5.5 fn:replace()

This function is used to replace a given string from a new string in source string.

Syntax of the function is :

String fn:replace(String inputstring, String replaceToString,String replacefromString)

Example

create fnreplace.jsp to demonstrate the fn:replace function like below.

<%@ tagliburi="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ tagliburi="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>fn:escape example</title>
</head>
<body>
Input String with escapeXml :: ${fn:escapeXml('<i>Hello <strong>I am </strong> Input String<i>')}
<br/>
Input String without escapeXml :: ${'<i>Hello <strong>I am </strong> Input String<i>'}
</body>
</html>

Access fnreplace.jsp by hitting http://localhost:8080/jsp-tutorial/fnreplace.jsp

20.5.6 fn:toLowerCase() , fn:toUpperCase() –

These functions are used to change the case of given string

Syntax of the function is :

String fn:toLowerCase(String inputstring)

String fn:toUpperCase(String inputstring)

Example

create fnchangeCase.jsp to demonstrate the fn:toLowerCase and fn:toUpperCase function like below.

<%@ tagliburi="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ tagliburi="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>fn:replace example</title>
</head>
<body>
<c:setvar="inputString" value="Hello I am Input String"/>
${fn:replace(inputString,'Input','Replaced')}
</body>
</html>

Access fnchangeCase.jsp by hitting http://localhost:8080/jsp-tutorial/fnchangeCase.jsp

20.5.7 fn:trim()

This function is used to trim the leading and trailing saces from the given string

Syntax of the function is :

String fn:trim(String inputstring)

Example

create fntrim.jsp to demonstrate the fn:trim function like below.

<%@ tagliburi="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ tagliburi="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>fn:toLowerCase and fn:toUpperCase example</title>
</head>
<body>
<c:setvar="inputString" value="Hello I am Input String"/>
LowerCaseOutput :: ${fn:toLowerCase(inputString)}
<br/>
UpperCaseOutput :: ${fn:toUpperCase(inputString)}
</body>
</html>

Access fntrim.jsp by hitting http://localhost:8080/jsp-tutorial/fntrim.jsp

20.5.8 fn:subtring()

This function is used to get a portion of string

Syntax of the function is :

String fn:substring(String inputstring, int beginIndex,int endIndex)

Example

create fnsubstring.jsp to demonstrate the fn:substring function like below.

<%@ tagliburi="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ tagliburi="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>fn:trim example</title>
</head>
<body>
<c:setvar="inputString" value="  Hello I am Input String  "/>
Input  :: ${inputString}
<br/>
Trimmed Output :: ${fn:trim(inputString)}
</body>
</html>

Access fnsubstring.jsp by hitting http://localhost:8080/jsp-tutorial/fnsubstring.jsp

20.5.9 fn:subtringBefore(),fn:subtringAfter()

These functions are used to get a portion of string after or before provided string

Syntax of the function is :

String fn:substringAfter(String inputstring, String afterString)

String fn:substringBefore(String inputstring, String beforeString)

Example

create fnsubstringAfter.jsp to demonstrate the fn:substringAfter and fn:substringBefore function like below.

<%@ tagliburi="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ tagliburi="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>fn:substring example</title>
</head>
<body>
<c:setvar="inputString" value="Hello I am Input String"/>
${fn:substring(inputString,6,17)}
</body>
</html>

Access fnsubstringAfter.jsp by hitting http://localhost:8080/jsp-tutorial/fnsubstringAfter.jsp

20.5.10 fn:startsWith(),fn:endsWith()

These functions are used to check if the input String starts or ends with provided string. These functions returns boolean value (true or false )

Syntax of the function is :

boolean fn:startsWith(String inputstring, String prefixString)

boolean fn:endsWith(String inputstring, String suffixString)

Example

create fnstarts.jsp to demonstrate the fn:startsWith and fn:endsWith function like below.

<%@ tagliburi="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ tagliburi="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>fn:substring after and before example</title>
</head>
<body>
<c:setvar="inputString" value="Hello I am Input String"/>
Input String :: ${inputString }
<br/>
Before String 'am' :: ${fn:substringBefore(inputString,'am')}
<br>
After String 'am' :: ${fn:substringAfter(inputString,'am')}
</body>
</html>

Access fnstarts.jsp by hitting http://localhost:8080/jsp-tutorial/fnstarts.jsp

20.5.11 fn:length()

This function is used to determine the length of string

Syntax of the function is :

int fn:length(String inputstring)

Example

create fnlength.jsp to demonstrate the fn:length function like below.

<%@ tagliburi="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ tagliburi="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>fn : startsWith and endsWith example</title>
</head>
<body>
<c:setvar="inputString" value="Hello I am Input String"/>
Input  :: ${inputString}
<br/>
Is String starts with 'Hello' :: ${fn:startsWith(inputString,'Hello')}
<br/>
Is String ends with 'String' :: ${fn:endsWith(inputString,'String')}
</body>
</html>

Access fnlength.jsp by hitting http://localhost:8080/jsp-tutorial/fnlength.jsp

20.5.12 fn:join(),fn:split()

These functions are used to concatenate the array of Strings or split the String based on a delimeter

Syntax of the function is :

String fn:join(String[] inputstring, String delimeter)

String[] fn:split(String inputstring, String delimeter)

Example

create fnjoin.jsp to demonstrate the fn:join and fn:split function like below.

<%@ tagliburi="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ tagliburi="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>fn:length example</title>
</head>
<body>
<c:setvar="inputString" value="Hello I am Input String"/>
Input  :: ${inputString}
<br/>
Length of Input String is :: ${fn:length(inputString)}
</body>
</html>

Access fnjoin.jsp by hitting http://localhost:8080/jsp-tutorial/fnjoin.jsp

20.6 SQL Tags

JSTL SQL tag library can be used to interact with database. Tab library provides tag to perform several operations on database.

· URI for format tag library is http://java.sun.com/jsp/jstl/sql

· To use SQL tag library on JSP page we need to import its tag library using

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

We will discuss details of SQL tags in next chapter on JSP-Database Access

20.7 Internationalization tag library (Formatting Tags)-

Internationalization (i18n) is way to serve users in multiple different languages including formatting of numbers, adjustment to date and time etc. Formatting tags provides several tags (functions) that can be used to make our web application to support i18n

· URI for format tag library is http://java.sun.com/jsp/jstl/fmt

· To use format tag library on JSP page we need to import its tag library using

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

20.7.1 <fmt:setBundle>

Resource bundle is nothing but a collection of messages in a form of key – value pair. We can define one file per locale and all file will have same keys but corresponding values will be locale specific. This tag is used to load the resource bundle in any scoped variable OR in bundle configuration variable.

Syntax of tag is –

<fmt:setBundle basename="bundle_base_name" var="variable_name" scope=”page| request | application| session”/>

· basename- is mandatory attribute and used to specify base name of bundle.

· var- is an optional attribute to store the bundle

· scope- is the scope of variable defined in var tag. Default value is page

20.7.2 <fmt:setLocale>

This tag is used to store the locale in locale configuration variable. Syntax of this tag is-

<fmt:setLocale value="valid_locale" variant="variant" scope=”page| request | application| session”/>

· value -is the mandatory attribute and is used for specifying a locale. To specify a locale code it must have made from the two-letter (lower case) language code (according to ISO-639) and/or the two-letter (upper-case) of country code (according to the ISO-3166). And if both the language and country code are used for specifying the locale the locale code must be separated by either hyphen (-) or underscore (_).

· variant : is an optional attribute and is used for specifying the browser or vendor specific variant.

· scope : is an optional attribute and specifies the scope of locale configuration variable. Default value is Page

20.7.3 <fmt:message>

This tag is used to display the messages from bundle based on the locale. Syntax of this tag is-

<fmt:message key="key_in_bundle_file" bundle="bundle_variable" scope=”page| request | application| session” var=”variable_name”/>

· var- is an optional attribute to store the resolved localized message.

· scope- is the scope of variable defined in var tag. Default value is page

· key- is the key in resource bundle whose value to display

· bundle- variable in which bundle is stored and this bundle will be used.

Note : If var is used, then message will not be displayed, instead will be stored in a variable for a later use.

Example-

To see above format tags in action , lets create resource bundle in two locale (default – without locale and another in Spanish locale (es_US).

Resource bundle is simply a properties file and for all files , base name will remain same and all locales will be appended to base name.

1)Create Following two files in com.sample.jsp.tutorial.bundle

myBundle.properties

title=Title in Default Locale

usernameLbl=Username in default Locale

passwordLbl=Password

myBundle_es_US.properties

title=Title in Spanish Locale

usernameLbl=Username in Spanish

passwordLbl=Password- in Spanish

2)Create fmtlocale.jsp

<html>
<head>
<title>fmt:locale, fmt:messageand fm:setBundle tag example </title>
</head>
<%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt"   uri="http://java.sun.com/jsp/jstl/fmt" %>
<body>
<fmt:setBundlebasename="com.sample.jsp.tutorial.bundle.myBundle" var="resourceBundle"/>
Title in default Locale :: <fmt:message key="title" bundle="${resourceBundle}"/>
<br/>
<fmt:message key="usernameLbl" var="userMsg" bundle="${resourceBundle}"/>
Username in Default Locale :: <c:out value="${userMsg}"/>
<br/>
<fmt:setLocale value="es_US"/>
<fmt:setBundlebasename="com.sample.jsp.tutorial.bundle.myBundle" var="resourceBundle"/>
Title in Spanish Locale :: <fmt:message key="title" bundle="${resourceBundle}"/>
<br/>
Username in Spanish Locale :: <fmt:message key="usernameLbl" bundle="${resourceBundle}"/>
</body>
</html>

Access fmtlocale.jsp by hitting http://localhost:8080/jsp-tutorial/fmtlocale.jsp

20.7.4 <fmt:formatNumber>

This tag is used to format the numbers, currency and percent. Syntax of this tag is-

Most commonly used attributes of this tag are-

· value- is the value which needs to fromat

· type- possible values are CURRENCY, NUMBER and PERCENT . Default value is NUMBER

· var- is an optional attribute to store the resolved localized message.

· scope- is the scope of variable defined in var tag. Default value is page

· maxIntegerDigits- maximum number of integer digits to display

· maxFractionDigits- maximum number of fractional digits to display

· minIntegerDigits- minimum number of integer digits to display

· minFractionDigits- minimum number of fractional digits to display

Example

Create fmtformatNumber.jsp with below content

<%@ 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>JSTL fmt:formatNumber Tag</title>
</head>
<body>
<c:setvar="input" value="140476.9319" />
<fmt:setLocale value="en_US"/>
<p>Format Number : <fmt:formatNumber value="${input}"/></p>
<p>Format Currency :<fmt:formatNumber value="${input}" type="CURRENCY"/></p>
<fmt:formatNumber
            maxIntegerDigits="3"
            value="${input}"
maxFractionDigits="3"
minFractionDigits="1"
minIntegerDigits="1"
var="formattedValue"
           />
<p>Formatted Number with other Attributes : <c:out value="${formattedValue}"></c:out>
</body>
</html>

Access fmtformatNumber.jsp by hitting http://localhost:8080/jsp-tutorial/fmtformatNumber.jsp

Like us on Facebook