AN INTRODUCTION
TO JSTL

PART-2 -CORE TAGS IN JSTL
Tutorial
Home |
Part
1 | Part 2 | Part
3 | Part 4
In the second part of this tutorial on JSTL, the author
explains how the tags in the core-group can be used in JSP pages, with a number
of simple
examples.
We are now ready to experiment with all the tags in the ‘core’
library. The core tags have the following uniform ‘uri’.
‘http://java.sun.com/jstl/core'
( However, in the book by Hans Bergsten titled,"Java Server
Pages" ( third edition),
(OReilly pub)the uri is consistently given as :
'http://java.sun.com/jsp/jstl/core'.It looks as if there has been some
change in specification and grammar, after it was published!
This appears to be wrong as the server threw exception.The
correct uri is :
'http://java.sun.com/jstl/core'.)
The prefix is ‘c:’
The following tags are available in the
‘core’ library.
( Remember them as a dozen!).
<c:set
<c:out
<c:if test=
“
<c:choose , <c:when
,
<c:otherwise
<c:forEach
<c:forTokens
<c:import
<c:url
<c:redirect
<c:param
-----------------------------------------------
We will now see simplest illustrations for the above
tags.There are a dozen demos, to bring out the features of each of these
tags.
---------------------------------------
demo1.jsp uses <c:set
& <c:out tags.
:We create demo1.jsp as:
e:\tomcat5\webapps\root\demo1.jsp
-----------------------------------------
// demo1.jsp
<%@ page contentType="text/html" %>
<%@ taglib
prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<body
bgcolor=lightblue>
<form method=post
action="demo1.jsp">
NAME <input type=text name="text1"><br>
PLACE<input type=text name="text2"><br>
<input
type=submit>
</form>
NAME:<c:out value="${param.text1}" /><br>
PLACE:<c:out value="${param.text2}" />
</body>
</html>
----------------------------------------------
In all the previous examples, we invoked the JSP file
through a html
file. But, in demo1.jsp, we are posting the page to itself.( in asp.net
style!).( but there is no 'retention of data' , unlike asp.net).
We start Tomcat5, and type the url as :
‘http://localhost:8080/demo1.jsp’. in the browser.We
get a form with two text boxes and a submit button. We fill up the textboxes
with ‘name’ and ‘place’ and submit.
The demo1.jsp executes and displays the values entered by
the user.due to the JSTL tags
<c:out value=”${param.text1}
/>
etc.
That is about our first and introductory example.
----------- ---
The second example is very important.
When the user enters data in a number of fields, it is tedious to collect the
data and transfer it to jsp page for processing. In our example, we are
collecting data about a player, such as his name, place and game. We can have
much more but we are restricting for space considerations. JSP has an action tag
, known as 'jsp:setProperty'. Using this along with
a standard javabean, we can extract data and transfer it to our program in a
single step.
The syntax is
<jsp:useBean
id="bean1"
class="ourbeans.player" >
<jsp:setProperty
name="bean1"
property="*" />
</jsp:useBean>
:( the * sign denotes 'all').
-----
But, we should first create the 'player
' bean with all the attributes and getter & setter
methods, as shown.
---------------------------------------------
// player.java
package ourbeans;
public class player{
String
name;
String
place;
String
game;
public player(){
name=" ";
place=" ";
game=" ";
}
//---------------------------
public void
setName(String a){
name=a;
}
public void setPlace(String b){
place=b;
}
public void setGame(String c){
game=c;
}
//------------------------------
public String getName(){
return name;
}
public String getPlace(){
return place;
}
public String getGame(){
return game;
}
}
---------------------------------
In demo2.jsp, we collect the data and
then display
the data entered by the user.
Note that instead of {param.text1}, we are using
{bean1.name}. We should carefully name the html form controls with the
corresponding attribute names given in the bean. We cannot name the controls as
'text1' etc, now!
<c:out value="${bean1.name}" />
<c:out value="${bean1.place}" />
<c:out value="${bean1.game}" />
---
We get correct result.
=============================================
// demo2.jsp
<%@ page contentType="text/html" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<html>
<body>
<form method=post action="demo2.jsp">
<jsp:useBean id="bean1" class="ourbeans.player">
<jsp:setProperty name="bean1" property="*" />
</jsp:useBean>
Name <input type=text name="name"><br>
Place<input type=text name="place"><br>
Game<input type=text name="game"><br>
<input type=submit>
</form>
Name: <c:out value="${bean1.name}" /><br>
Place: <c:out value="${bean1.place}" /><br>
Game: <c:out value="${bean1.game}" />
</body>
</html>
=============================================
Once again, it will be noticed that there is no java code
in this example, as everything is being done by tags, only..
***********************************************
We are now ready to take up examples for 'condition' tags.
There are two types of 'condition tags'.
namely, <c:if>
&
<c:choose>.
In the
third demo, we learn how to use the <c:if tag.
----------------------------------------------
//demo3.jsp
<%@ page contentType="text/html" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<html>
<body
bgcolor=lightblue>
<form method=post
action=demo3.jsp>
<select
name="combo1">
<option
value="sam">sam
<option
value="tom">tom
</select>
<input type=submit>
</form>
<c:set
var="s" value="${param.combo1}" />
<c:out value="${s}" />
<br>
<c:if test="${s eq
'sam' }"
>
<c:out value="Good
Morning...SAM!"
/>
</c:if>
<c:if test="${s = = 'tom'}"
>
<c:out value=" How Are
You?....TOM!"
/>
</c:if>
</body>
</html>
-----------------------------------------
There is a combo with two options, namely
'sam' and 'tom'. If the user selects 'sam' and submits the
form, he gets 'GoodMorning ...SAM!". If he selects 'tom' instead, he gets
'How are you..TOM?'.
The above code is no ‘Rocket-Science’ as American authors say!But ,
if we are careless
in typing the names ‘sam’ or ‘tom’ in the test condition, we could spend
hours together , trying to coax this code into functioning! We should not leave
space after the single quote in the 'test expression'. Second point worth
noting in the above example is that we can use either == ( double equal to) or
eq to test
equality.
***********************************************
In the fourth example which
follows, we take up <c:choose> tag.
The syntax is:
<c:choose >
<c:when
test="
" >
<c:otherwise> something </c:otherwise>
</c:choose>
The peculiarity to be noted here is that unlike
<c:if , where we had to explicitly use <c:out for printing , no such <c:out has been used here., and yet the result
is displayed,
because 'choose' includes 'displaying'..
When we choose '7', "select between
1 & 5 " will be displayed!
-----------------------------------------------
// demo4.jsp
<%@ page contentType="text/html" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<html>
<body
bgcolor=lightblue>
<form
method=post
action="demo3.jsp">
<select name="combo1">
<option
value="1">1
</option>
<option
value="2">2
</option>
<option
value="3">3
</option>
<option
value="4">4
</option>
<option
value="5">5
</option>
<option
value="7">7
</option>
</select>
<input
type=submit>
<c:set var="s" value="${param.combo1}" />
Today is
<br>
<font size=24 color=red>
<c:choose>
<c:when test="${s==1}">Sunday </c:when>
<c:when test="${s==2}">Monday</c:when>
<c:when test="${s==3}">Tuesday</c:when>
<c:when
test="${s==4}">Wednesday</c:when>
<c:when test="${s==5}">Thursday</c:when>
<c:otherwise>
select between 1 & 5
</c:otherwise>
</c:choose>
</body>
</html>
---------------------------------------------
Demo-5 deals with Iteration tag.
We are familiar with the 'for-each' construct.
JSTL's 'for-each' also has the same functionality.
In the following example, we have a String array. named as
'colors'.
By using the <c:forEach> tag, we
iterate through the array and display the values.
------------------------------------
//demo5.jsp
<%@ page contentType="text/html" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<%
pageContext.setAttribute("colors",
new String[]
{"red","green","blue","orange","black"} ); %>
<table>
<c:forEach var="n" items="${colors}"
varStatus="a">
<tr>
<td> <c:out value="${a.index}" /> </td> <td> <c:out
value="${a.count}" /> </td>
<td> <c:out value="${a.first}" /> </td>
<td> <c:out value="${a.last}" /> </td>
<td> <c:out value="${a.current}" /> </td>
<tr>
</c:forEach>
</table>
We get the following display, when we execute the
program.
|
0
|
1
|
true |
false |
red
|
|
1
|
2
|
false |
false |
green |
|
2
|
3
|
false |
false |
blue |
|
3
|
4
|
false |
false |
purple |
|
4
|
5
|
false |
true |
black |
===============================================
<c:forEach> action tag contain the following
attribute list:
items
: the collection of items like String[]
var : a symbolic name for
the collection
begin
: the starting index of iteration
end
: the ending index of iteration
step
: incremental step
varStatus:
symbolic name for current status.
If we assign the symbolic name 'a' for the status, we are
able to access its properties such as index, count, whether it is first item,
whether it is last item and the current value.
Demo6 also
deals with iteration tag.In the following example, the iteration starts at value
3 and ends at value 8 .It displays the values of n in each iteration.Each
iteration increments the value of n automatically by 1,if step is not specified.
-----------------------------------------------
demo6.jsp
<%@ page contentType="text/html" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<c:forEach var="n" begin="3" end="8" >
<c:out value="${n}" /> <br>
</c:forEach>
===============================================
Demo7
deals with JSTL's 'forTokens'
tag.<c:forTokens>, which iterates over a string of tokens separated by a
set of delimiters like the stringTokenizer class in Java.
--------------------------------------------
demo7.jsp
<%@ page contentType="text/html" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<c:set var="s" value="SAM,DELHI,MCA,24,90" />
<html>
<body>
<table border="1">
<tr>
<th>Name</th>
<th>Place</th>
<th>Degree</th>
<th>Age</th>
<th>Mark</th>
</tr>
<tr>
<c:forTokens items="${s}"
delims="," var="token" >
<td><c:out
value="${token}"
/></td>
</c:forTokens>
</tr>
</table>
<br>
</font>
</body>
</html>
------------------------------------------
|
Name
|
Place
|
Degree
|
Age
|
Mark
|
|
kala |
Kkdi |
mca |
23 |
87
|
------------------------------------------
The essential attributes of 'forTokens' tag are:
Attribute
Description
items
The string to
tokenize
delims The delimiter
characters
that separate
the tokens of
the string.
===============================================
Demo8 deals
with URL-Related actions.<c:import>action tag imports the conent of a
URL-based resource and provides a simple way to access URL-based resources that
can either be included or processed within the JSP..
In the
following example,the import action tag imports the content of welcome.htm file
here.So it displays the contents of demo8.jsp and welcome.htm.
--------------------------------------------
// welcome.htm
<html>
<body>
WELCOME
<br>
</body>
</html>
---------
demo8.jsp
<%@ taglib
prefix="c"
uri="http://java.sun.com/jstl/core" %>
<c:import url="welcome.htm"/>
<c:out value="to our web-site!" />
===============================================
In demo9 we discuss the <c:url> action tag.
<c:url> prints the value of URL.It is easier
to construct the hyperlinks.It is useful for session preservation
(URL-Encoding).
In the following example,we use <c:url> to make a
link to another html file. When we execute demo9, we get a link , with text
'send'.
When we click on the link, we are taken to welcome.htm.
--------------------------------------
demo9.jsp
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<a
href="<c:url
value="http://localhost:8080/welcome.htm/>">
send
</a>
--------------------------------------
===============================================
demo10 deals
with <c:redirect> action tag.
This tag forwards the browser to the specified URL .
demo10.jsp
<%@ page contentType="text/html" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<c:redirect
url="http://localhost:8080/welcome.htm />
===============================================
Finally, <c:param> tag is
useful to send some parameter value to the page to which redirect occurs. In our
example, the code redirects to sample.jsp, but it takes the param named 'a'
with value='SAM' , to the redirected page.
And, the sample.jsp accepts this param and prints it.
<c:out value="${param.name1}"/>
----------------------------------------------
demo11.jsp
<%@ page contentType="text/html" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<c:redirect url="http://localhost:8080/jstldemos/
core/sample.jsp" >
<c:param name="name1" value="SAM"/>
</c:redirect>
-------------------------------------------
( in a different file)
sample.jsp
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<c:out value="${param.name1}"/>
And to end our whirlwind tour of core-tags in JSTL, here is
a demo which mixes EL of JSP-2(Expression Language of JSTL) with 'Expression'
(also known as request-time Expression ) of JSP1.2.
demo12.jsp
<%@ page contentType="text/html" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
JSTL welcomes <br>
<c:out value="${param.text1}" />
<br>
JSP Expression welcomes
<%=request.getParameter("text1") %>
-------------------------------------------
In the next and third part of this tutorial, we will learn
about xml tags in JSTL.
Tutorial
Home |
Part
1 | Part 2 | Part
3 | Part 4

|
Current Comments
4 comments so far (post your own) View All Comments Latest 10 Comments:hey i need to call pageflow controller's action method usin <c:url>.. is that possible.. ie i need someway to call action method from <c:url>'s value attribute in portal 10..
Posted by vinutha on Tuesday, 03.4.08 @ 15:23pm | #51291
Yes, it's really good and simplified approach to understand but if u add output it's very nice.I'm requesting about my comment
Posted by uday on Tuesday, 07.17.07 @ 14:54pm | #21432
Hi,
really good introduction. I need some help with jstl. wrt demo7, if i have the first two columns from one list containing n rows(which is populated from a DB table) and the last 3 columns are from another list containing the same n rows(from another DB table),how do i get to display them
Thanks in advance/
Posted by jordan on Tuesday, 06.5.07 @ 12:00pm | #18262
it is really good
but if u add the output page along with the code u given
then it will be more good
Posted by prasad on Friday, 02.16.07 @ 11:31am | #8037