Nearby lessons

28 of 30

JSP Examples - JSTL

📌 What You Will Learn
  • Set up JSTL in a web app
  • Use the core tag library without scriptlets
  • Iterate collections with c:forEach and c:forTokens

JSTL core library programs: installing JSTL, general-purpose tags (c:out, c:set, c:remove, c:catch), conditional tags and the iteration tags (c:forEach, c:forTokens) with loop-status demos.

Installing JSTL

Defines several API classes defined by SUN.

Example01
JCode Cell
1 
2jstl.jar:
3

Installing JSTL

Contains implementation classes provided by vendor.

Note:

It is recommended to place these jar files at server level.(D:\Tomcat 7.0\lib) instead of application

level.

To make core library available to JSP, we have to declare taglib directive as follows...

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

Example02
JCode Cell
1 
2standard.jar:
3

General Purpose Tags

<c:out>

where c is prefix and out is name of tag.

This tag can be used for writing Template text data and expressions to the JSP.

form-1:

<c:out value="durga" />

It prints durga to JSP

<c:out value="${param.user}" />

It prints the value of request parameter user to the JSP.

form-2:

If the main value is not available or it evaluates to null, then we can provide default value by using

default attribute.

<c:out value="${param.user}" default="Guest" />

test.jsp:

Example03
JCode Cell
1 
2<c:out>:
3

General Purpose Tags

o/p: Hello Guest

Note:

<c:out> can accept the following 3 attributes

1.value

2.default

3.escapeXml

2.<c:set>

We can use <c:set> to set attributes in any scope and to set map and bean properties also.

Form-1:

We can use <c:set> to set attributes in any scope

<c:set var="name of the attribute" value="attributevalue" scope="request" />

scope attribute is optional and default scope is page scope.

test.jsp:

Example04
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3<H1>Hello.. <c:out value="${param.uname}" default="Guest" />http://localhost:7777/jsp1/test.jsp
4

General Purpose Tags

We can set map or bean properties by using <c:set>

We can specify bean or map object by using target attribute and property name by using property

attribute.

<c:set target="customer" property="name" value="pavan" />

The attributes for the <c:set> are:

1.var 2. value 3. scope 4. target 5. property

Example05
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3<c:set var="x" value="10" scope="request" />
4<c:set var="y" value="20" scope="request" />
5<c:set var="sum" value="${x+y}" scope="session" />
6<h1>The result is : <c:out value="${sum}" /></h1>form-2:
7

General Purpose Tags

To remove attributes in the specified scope we can use this tag.

This tag can take the following 2 attributes

1.var Name of the Attribute

2.scope The Scope in which Attribute Present

Eg: <c:remove var="x" scope="session" />

If the scope is not specified then the container will search in the page scope for the required

attribute. If the attribute is not available then it will search in the request scope followed by

session and application scopes.

test.jsp:

Example06
JCode Cell
1 
2<c:remove>:
3

General Purpose Tags

This tag can be used to catch an exception within the JSP instead of forwarding to the error page.

The risky code we have to place as the body of <c:catch>

<c:catch >

Risky Code

</c:catch>

If an exception raised in the risky code, then this tag suppresses that exception and rest of the JSP

will be executed normally.

If an exception is raised we can hold that by using var attribute, which is the page scoped

attribute.

test.jsp:

Example07
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3<c:set var="x" value="40" scope="page" />
4<c:set var="y" value="60" scope="page" />
5<c:set var="sum" value="${x+y}" scope="session" />
6<h1>
7The result is : <c:out value="${sum}" /><br>
8<c:remove var="x" />
9<c:remove var="y" />
10<c:remove var="sum" />
11The result is : <c:out value="${sum}" default="1000"/><br>
12</h1><c:catch>:
13

General Purpose Tags

http://localhost:7777/jsp1/catch1.jsp?uname=pavan&uage=47&uheight=5.11

User Name: pavan

Age:47

User Height: 5.11

http://localhost:7777/jsp1/catch1.jsp?uname=pavan&uage=ten&uheight=5.11

User Name: pavan

Oops ...Exception raised : java.lang.NumberFormatException: For input string: "ten"

User Height: 5.11

Summary of General Purpose Tags

TagDescriptionAttribute
1) <c:out>Value, default,
For writing ExpressionescapeXml
2) <c:set>and Template Text to JSPvar, value, scope

target, property

Example08
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3<h1>
4User Name: ${param.uname}<br>
5<c:catch var ="e" >
6<%
7int age = Integer.parseInt(request.getParameter("uage"));
8%>
9Age:${param.uage}<br>
10 
11</c:catch>
12<c:if test="${ e != null}">
13Oops ...Exception raised : ${e}<br>
14</c:if>
15User Height: ${param.uheight}
16 
17</h1>
18

General Purpose Tags

For removing Attributes

For supporting an Expression

and to continue rest of Jsp

normally

Conditional Tags

1.<c:if>

We can use to implement core java if statement.

There are 2 forms are available for <c:if>

without body:

<c:if test="test_condition" var="x" scope="session"/>

In this case test_condition will be evaluated & store its results into variable x. In the rest of the

page where ever this test condition is required, we can use directly its value without evaluating

once again.

In this case both test and var attributes are mandatory. scope attribute is optional and default

scope is page.

test.jsp:

Example09
JCode Cell
1 
2<c:remove> To set Attributes, Bean var, scope
3<c:catch> OR Map Propertiesvar
4

General Purpose Tags

If test_condition is true then body will be executed otherwise without executing body rest of the

jsp will be continued.

In this case also we can store test result inside var attribute.

Both var and scope attributes are optional but test attribute is mandatory.

test.jsp:

Example10
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3<c:set var="x" value="10" scope="request"/>
4<c:if test="${x ne '10'}" var="y" scope="session" />
5 
6<h1>
7X value is : ${x}<br>
8Test Result is : ${y}
9</h1>with body:
10<c:if test="test_condition" var="x" scope="session">
11body
12</c:if>
13

General Purpose Tags

<c:choose>,<c:when> and <c:otherwise>:

We can use these tags for implementing if-else and switch statements.

  • Implementing if-else:

JSTL does not contain any tag for else. We can implement if-else statement by using above tags.

Example11
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3<c:set var="x" value="10" scope="request"/>
4 
5<c:if test="${x eq '10'}" >
6<h1> x value is equal to 10</h1>
7</c:if>
8

General Purpose Tags

If test condition is true,Action-1 will be executed otherwise Action-2 will be executed.

Implementing switch statement:

Example12
JCode Cell
1 
2<c:choose>
3 
4<c:when test="test_condition">
5Action-1
6</c:when>
7 
8<c:otherwise>
9Action-2
10</c:otherwise>
11 
12</c:choose>
13

General Purpose Tags

1.<c:choose> should compulsory contains at least one <c:when> tag. But <c:otherwise> is optional.

  • Every <c:when> tag implicitly contains break statement. Hence there is no chance of fall through

inside switch.

  • We should take <c:otherwise> as last case only.

4.<c:choose> and <c:otherwise> won't take any attributes but <c:when> can take one mandatory

attribute test.

switch.jsp:

Example13
JCode Cell
1 
2<c:choose>
3<c:when test="test_condition1">
4Action-1
5</c:when>
6<c:when test="test_condition2">
7Action-2
8</c:when>
9.....
10<c:when test="test_conditionN">
11Action-N
12</c:when>
13<c:otherwise>
14Default Action
15</c:otherwise>
16</c:choose>
17

General Purpose Tags

Example14
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3<h1> Select The Number:
4<form action="/jsp1/switch.jsp">
5<select name="combo" >
6<option value="1">1</option>
7<option value="2">2</option>
8<option value="3">3</option>
9<option value="4">4</option>
10<option value="5">5</option>
11<option value="6">6</option>
12<option value="7">7</option>
13</select>
14<input type="submit"/>
15</form>
16<c:set var="s" value="${param.combo}"/>
17

16) Today is

Summary of Conditional Tags

TagDescriptionAttributes
1) <c:if>To implement Coretest, var, scope

Java if - Statement

Example15
JCode Cell
1 
2<c:choose>
3<c:when test="${s == 1}" >Sunday</c:when>
4<c:when test="${s == 2}" >Monday</c:when>
5<c:when test="${s == 3}" >Tuesay</c:when>
6<c:when test="${s == 4}" >Wednesday</c:when>
7<c:when test="${s == 5}" >Thursday</c:when>
8<c:otherwise>Select between 1 and 5</c:otherwise>
9</c:choose>
10</h1>
11

16) Today is

<c:when>To implementdoesn't contain any Attribute
<c:otherwise> if - else &but <c:when> should contain
switch stmtonly one Attribute i.e. test
Example16
JCode Cell
1 
2<c:choose> <c:choose> & <c:otherwise>
3

Iteration Tags

1.<c:forEach>:

form-1:

Example17
JCode Cell
1 
2<c:forEach> General purpose for loop
3<c:forTokens> Specialized for splitting Strings similar to StringTokenizer.
4

Iteration Tags

o/p: 10 times

<c:forEach begin="1" end="10" step="2" > 5 times

<c:forEach begin="4" end="0" step="-1" > Invalid b'z step value should not be -ve.

The begin attribute specifies the index where the loop has to start.

end index specifies the index where the loop has to terminate.

This loop internally maintains counter variable which is incremented by step attribute value.

The default value of step attribute is "1" and it is optional attribute.

form-2: <c:forEach> with var attribute:

<c:forEach> internally maintains a counter variable which can be accessed by using var attribute.

This variable is local variable. Hence from outside of for loop we cannot access.

test.jsp:

Example18
JCode Cell
1 
2<c:forEach begin="1" end="10" step="1" >
3<h1>Learning JSTL is very easy!!!!</h1>
4</c:forEach>
5

Iteration Tags

Example19
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3

Iteration Tags

Form-3: <c:forEach> for iterating through Arrays and Collections:

Example20
JCode Cell
1 
2<c:forEach begin="1" end="10" step="2" var="count">
3<h1>Learning JSTL is very easy!!!!---${count}</h1>
4</c:forEach>
5

Iteration Tags

Items attribute should be either Collection object or array object. This action will iterate over each

item in the collection until all elements completion.

We can represent current collection object by using var attribute.

Q. Write a JSP to print all elements of array:

test.jsp:

Example21
JCode Cell
1 
2<c:forEach items="collection object" var="current object">
3 
4body
5 
6</c:forEach>
7

Iteration Tags

Q. Write a JSP to print all request headers information:

test.jsp:

Example22
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3<%
4String[] s = {"A","B","C","D"};
5pageContext.setAttribute("s",s);
6%>
7 
8<c:forEach items="${s}" var="obj">
9<h1>The current Object is : ${obj} </h1>
10</c:forEach>
11

Iteration Tags

Q. Write a JSP to print all request parameters information:

test.jsp:

Example23
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3 
4<table border="2">
5<c:forEach items="${header}" var="hdr" >
6<tr> <td>${hdr.key}</td><td>${hdr.value}</td></tr>
7</c:forEach>
8</table >
9

Iteration Tags

http://localhost:7777/jsp1/forEach2A.jsp?user=durga&age=48&course=java&mail=durgaocjp@g

mail.com

Form-4: <c:forEach> with varStatus attribute:

varStatus attribute describes the status of iteration like current iteration number, is it first

iteration or not etc..

This attribute is of type javax.servlet.jsp.core.TagLoopStatus.

This class contains several methods which are useful during iteration.

Example24
JCode Cell
1 
2<table border="2">
3<c:forEach items="${param}" var="p" >
4<tr> <td>${p.key}</td><td>${p.value}</td></tr>
5</c:forEach>
6</table >
7

Iteration Tags

returns the current item

Example25
JCode Cell
1 
2Object getCurrent()
3

Iteration Tags

returns the current index(counter value)

Example26
JCode Cell
1 
2int getIndex()
3

Iteration Tags

returns the number of iterations that have already performed.

Example27
JCode Cell
1 
2int getCount()
3

Iteration Tags

Returns true if the current Iteration is the First iteration

Example28
JCode Cell
1 
2boolean isFirst()
3

Iteration Tags

Returns true if the current iteration is the last iteration

Example29
JCode Cell
1 
2boolean isLast()
3

Iteration Tags

returns start index

Example30
JCode Cell
1 
2Integer getBegin()
3

Iteration Tags

returns end index

Example31
JCode Cell
1 
2Integer getEnd()
3

Iteration Tags

returns the incremented value

test.jsp:

Example32
JCode Cell
1 
2Integer getStep()
3

Iteration Tags

It is a specialized version of forEach to perform StringTokenization based on some delimiter. It acts

as StringTokizer. We can store current token by using var attribute.

test.jsp:

Example33
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3 
4<c:forEach items="durga,pavan,ravi,shiva" varStatus="status" >
5 
6<h1>Is it First Iteration: ${status.first}<br>
7The current Object is : ${status.current}<br>
8The number of iterations already completed:${status.count}<br>
9Is it Last Iteration :${status.last}<br><hr></h1>
10 
11</c:forEach><c:forTokens>:
12

Iteration Tags

<c:forTokens> can accept the following extra attributes also.

begin: specifies the index at which iteration should start. The index of first token is zero.

end: specifies the index where iteration should be terminated.

step: counter increment value between iterations

varStatus: to specify the status of iteration

test.jsp:

Example34
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3 
4<c:forTokens items="durga.pavan.shiva.ravi" delims="." var="x">
5<h1>Hello:${x}<br>
6</c:forTokens>
7

Iteration Tags

Note:

In the case of <c:forTokens>, items attribute should be String only. But in the case of <c:forEach>

the items attribute can be Collection, Array, Map OR String. Hence <c:forTokens> is considered as

specialized version of <c:forEach>

Summary of Iteration Tags

TagDescriptionAttributes
1) <c:foreach>General Purpose for Loopitems, begin, end, step, var, VarStatus
Example35
JCode Cell
1 
2<%@ page isELIgnored="false" %>
3<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
4 
5<c:forTokens items="one,two,three,four,five,six" delims="," var="x" begin="2" end="5" step="1">
6<h1>Current Token is : ${x}</h1>
7</c:forTokens>
8

Iteration Tags

VarStatus

URL Related Tags

Example36
JCode Cell
1 
2<c:forTokens> Specialized for String Tokenization items, delims, begin, end, step, var,
3

Iteration Tags

We can use this tag for importing the response of other pages in the current page response at

request processing time.(i.e. Dynamic Include)

Form-1:

<c:import url="second.jsp" />

Eg:

first.jsp:

Example37
JCode Cell
1 
2<c:import>
3

Iteration Tags

<h1>The FREE videos are available in www.durgasoftvideos.com</h1>

Form-2:

We can import response from outside of current application also.(i.e cross context communication

is possible)

<c:import url="/second.jsp" context="/webapp2" />

url and context should be specified with absolute Path only i.e should starts with "/"

Form-3:

We can store the result of imported page into a variable specified by var attribute. In the rest of

the JSP, where ever that result is required we can use directly that variable without importing

once again.

test.jsp:

Example38
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3<c:import url="second.jsp" />second.jsp:
4

Iteration Tags

Another way to store the result of <c:import> is to use Reader object. It is alternative to var

attribute.

<c:import url="second.jsp" varReader="r" scope="page" />

Form-5:

While performing import we can send form parameters also to the Target JSP. For this we have to

use <c:param> tag. These parameters are available as form parameters in the target JSP.

first.jsp:

Example39
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3 
4<c:import url="second.jsp" var="x" scope="request" />
5${x}<br>
6${x}<br>
7${x}<br>
8${x}<br>
9${x}<br>Form-4:
10

Iteration Tags

<h1> The offered courses are : ${param.c1} and ${param.c2}

2.<c:redirect>:

This action can be used to redirect the request to another page. This is exactly similar to

sendRedirect() method of ServletResponse.

Form-1:

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

url can be specified either with Relative path or absolute path

first.jsp:

Example40
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3 
4<c:import url="second.jsp" >
5<c:param name="c1" value="JAVA"/>
6<c:param name="c2" value="PHP"/>
7</c:import>second.jsp:
8

Iteration Tags

<h1>Hello this is Second JSP</h1>

If we send the request to first.jsp then second.jsp will provide response through redirection.

Form-2:

We can redirect request to some other web application's resources also.

<c:redirect url="/second.jsp" context="/webapp2" />

url and context should be specified with Absolute Path only i.e. should starts with "/"

Form-3:

While performing redirection we can pass form parameters also to target resource.

first.jsp:

Example41
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3<c:redirect url="second.jsp" />second.jsp:
4

Iteration Tags

The offered courses are : ${param.c1} and ${param.c2}

3.<c:url>

We can use this standard action to rewrite urls to append session information and form

parameters.

Form-1:

<c:url value="second.jsp" var="x" scope="request"/>

The encoded url with sessionid will be stored inside x.

test.jsp:

Example42
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3<c:redirect url="second.jsp" >
4<c:param name="c1" value="Java"/>
5<c:param name="c2" value="PHP"/>
6</c:redirect>second.jsp:
7

Iteration Tags

<c:url value="/second.jsp" context="/webapp2" var="x" scope="request"/>

Form-3:

Example43
JCode Cell
1 
2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3<c:url value="second.jsp" var="x" scope="request"/>
4<h1>The modified url:${x}</h1>Form-2:
5

Iteration Tags

<h1>Offered courses are : ${param.c1} and ${param.c2}</h1>

Summary of URL related Tags

TagDescriptionAttributes
1) <c:import>url, var, scope, varReader, context

For importing the Response of

Example44
JCode Cell
1 
2<c:url value="second.jsp" var="x" >
3<c:param name="c1" value="java" />
4<c:param name="c2" value="php" />
5</c:url>first.jsp:
6<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
7<c:url value="second.jsp" var="x" >
8<c:param name="c1" value="JAVA" />
9<c:param name="c2" value="PHP" />
10</c:url>
11<h1>The modified url is : ${x}<br>
12<a href="${x}">Click Here to go to Next Page</a>second.jsp
13

Iteration Tags

Web Components.

To rewrite URL for appending

Session Information &

Parameters (Form)

To send Parameters while

importing & redirecting.

Summary of All Core Library Tags:

Example45
JCode Cell
1 
2<c:redirect> other Pages at Request processing url, context
3<c:url> time. value, var, scope, context
4<c:param> To redirect the Request to other name, value
5

Iteration Tags

JSTL SQL Library

It defines several tags for communicating with database.

To make sql library available to JSP, we have to write the following taglib directive.

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

The following are important sql library tags

Example46
JCode Cell
1 
2<c:out> To display expression to the JSP
3<c:set> to set attributes and bean properties
4<c:remove> to remove attributes from the specified scope
5<c:catch> To suppress exception and continue rest of the jsp
6<c:if> To implement core java if statement
7<c:choose>,<c:when>,<c:otherwise> To implement if-else and switch statements
8<c:forEach> To implement general purpose for loop
9<c:forTokens> For string tokenization purpose
10<c:import> for dynamic include
11<c:redirect> for redirection
12<c:param> to send parameters while importing and redirecting
13<c:url> to append session information to the url for session management purpose
14

Iteration Tags

To create DataSource

Example47
JCode Cell
1 
2<sql:setDataSource>
3

Iteration Tags

To execute select query

Example48
JCode Cell
1 
2<sql:query>
3

Iteration Tags

To execute non-select query(insert|delete|update)

Example49
JCode Cell
1 
2<sql:update>
3

Iteration Tags

To provide parameter values in the case of PreparedStatement

Example50
JCode Cell
1 
2<sql:param>
3

Iteration Tags

To provide date values in the case of PreparedStatement

Example51
JCode Cell
1 
2<sql:dateParam>
3

Iteration Tags

To implement transactions

Note: Transaction is a group of sql queries which will be executed on the policy "Either ALL or

None".

Note: It is not recommended to use JSTL SQL Library in our JSP, b'z JSP meant for presentation

logic but not for business logic and database logic.

test.jsp:

Example52
JCode Cell
1 
2<sql:transaction>
3

Iteration Tags

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example53
JCode Cell
1 
2<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
3<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
4 
5<sql:setDataSource var="ds" driver="oracle.jdbc.OracleDriver"
6url="jdbc:oracle:thin:@localhost:1521:XE"
7user="scott" password="tiger"/>
8 
9<sql:query dataSource="${ds}" var="result">
10SELECT * from Employees
11</sql:query>
12<h1>
13<c:forEach items="${result.rows}" var="row" >
14${row.eno}--${row.ename}--${row.esal}--${row.eaddr}<br>
15</c:forEach>
16</h1>
17

Iteration Tags

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example54
JCode Cell
1 
2<sql:update dataSource="${ds}" var="count">
3insert into employees values(500,'Lasya',5000,'hyd')
4</sql:update>
5<h1>The number of rows inserted:${count}</h1>
6

Iteration Tags

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example55
JCode Cell
1 
2<sql:update dataSource="${ds}" var="count">
3delete from employees where esal>=4000
4</sql:update>
5<h1>The number of rows deleted:${count}</h1>
6

Iteration Tags

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example56
JCode Cell
1 
2<sql:update dataSource="${ds}" var="count">
3update employees set esal=7777 where ename='durga'
4</sql:update>
5<h1>The number of rows updated:${count}</h1>
6

Iteration Tags

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example57
JCode Cell
1 
2<sql:update dataSource="${ds}" var="count">
3update employees set esal=? where eno=?
4<sql:param value="9999"/>
5<sql:param value="100"/>
6</sql:update>
7<h1>The number of rows updated:${count}</h1>
8

Iteration Tags

JSTL Functional Library

It defines several tags for general String manipulation operations.

To use Functional Library,we have to write the following taglib directive in JSP.

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

The following are important tags in this library:

1.<fn:contains()>

Eg2:

split() method will split the given string according to the specified separator (delimiter)

join() method will join the given tokens into a single string based on separator.

Eg1:

Example58
JCode Cell
1 
2<sql:transaction dataSource="${ds}">
3 
4<sql:update>
5update employees set esalesal=esal-1000 where ename='durga'
6</sql:update>
7 
8<sql:update >
9update employees set esalesal=esal+1000 where ename='sunny'
10</sql:update>
11 
12</sql:transaction>
13

Iteration Tags

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Eg2:

Example59
JCode Cell
1 
2<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
3<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
4<c:set var="s" value="Hello Learning JSTL is Very Easy"/>
5<h1>
6Length: ${fn:length(s)}<br>
7In Upper Case: ${fn:toUpperCase(s)}<br>
8In Lower Case: ${fn:toLowerCase(s)}<br>
9Sub String from index 6 to 15: ${fn:substring(s,6,15)}<br>
10Is s contains JSTL: ${fn:contains(s,"JSTL")}<br>
11Is s starts with Hello: ${fn:startsWith(s,"Hello")}<br>
12Is s ends with Easy: ${fn:endsWith(s,"Easy")}<br>
13

Iteration Tags

Example60
JCode Cell
1 
2<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
3<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
4<c:set var="data" value="sunny,mallika,veena,reshmi,anasuya"/>
5 
6<c:set var="s" value="${fn:split(data,',')}"/>
7<h1>
8Result of split method:<br>
9<c:forEach items="${s}" var="s1">
10${s1}<br>
11</c:forEach>
12 
13<c:set var="result" value="${fn:join(s,'-')}"/>
14Result of Joining: ${result}
15
📝 Key Takeaways
  • JSTL needs the jstl + standard JARs and a taglib directive
  • c:forEach exposes items, var and varStatus
  • JSTL lets you drop scriptlets from view pages